[android] 新闻客户端实现左侧导航点击切换详解手机开发

设置主布局文件,为根布局设置一个id,作为内容区

 

ListView的条目设置点击事件,setOnItemClickListener()方法,参数:上下文

当前的Fragment实现OnItemClickListener接口

实现onItemClick()方法

switch判断一下当前菜单的索引位置,根据索引来实例化不同的Fragment对象

调用getActivity()方法,得到宿主Activity对象,这个activity暴露一个方法,替换fragment

使用关键字instances of判断getActivity()得到的是MainActivity类的实例

调用宿主Activity对象的switchMenuFragment()方法,切换内容部分的fragment,参数:Fragment对象

 

宿主activity暴露方法switchMenuFragment(),切换内容区fragment,参数:Fragment对象

getSupportFragmentManager().beginTransaction().replace(R.id.ll_main_content, fragment).commit();

调用SlidingMenu对象的toggle()方法,关闭左侧菜单

 

设置默认,当app第一次打开时显示导航第一个的内容

MainActivity

 

package com.qingguow.news; 
 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.view.Window; 
 
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu; 
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; 
import com.qingguow.news.fragment.IndexFragment; 
import com.qingguow.news.fragment.MenuFragment; 
 
public class MainActivity extends SlidingFragmentActivity { 
    private SlidingMenu sm; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        //设置滑动菜单 
        setBehindContentView(R.layout.menu); 
        setContentView(R.layout.activity_main); 
        sm=getSlidingMenu(); 
        sm.setMode(SlidingMenu.LEFT); 
        sm.setBehindWidth(200); 
        sm.setShadowDrawable(R.drawable.shape); 
        sm.setShadowWidth(5); 
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); 
         
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction(); 
        //替换导航fragemnt 
        MenuFragment menuFragment=new MenuFragment(); 
        ft.replace(R.id.ll_left_menu, menuFragment); 
        //替换内容fragment 
        IndexFragment index=new IndexFragment(); 
        ft.replace(R.id.ll_main_content, index).commit(); 
         
    } 
    /** 
     * 暴露方法切换内容区 
     * @param fragment 
     */ 
    public void switchMenuFragment(Fragment fragment){ 
        getSupportFragmentManager().beginTransaction().replace(R.id.ll_main_content, fragment).commit(); 
        sm.toggle(); 
    } 
}

 

MenuFragment.java

package com.qingguow.news.fragment; 
 
import java.util.ArrayList; 
import java.util.List; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
 
import com.qingguow.news.MainActivity; 
import com.qingguow.news.R; 
 
public class MenuFragment extends Fragment implements OnItemClickListener { 
    private View view; 
    //加载布局文件 
    @Override 
    public View onCreateView(LayoutInflater inflater, 
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
        view = inflater.inflate(R.layout.list_view, null); 
        return view; 
    } 
    //设置listview数据 
    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
        super.onActivityCreated(savedInstanceState); 
        ListView listview = (ListView) view.findViewById(R.id.lv_menu); 
         
        List<String> data=new ArrayList<String>(); 
        data.add("新闻"); 
        data.add("发现"); 
        data.add("关注"); 
        ListAdapter adapter = new ArrayAdapter<>(getActivity(), 
                android.R.layout.simple_list_item_1, android.R.id.text1, 
                data); 
        listview.setAdapter(adapter); 
        listview.setOnItemClickListener(this); 
         
    } 
    /** 
     * 导航点击 
     */ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
            long id) { 
        Fragment fragment=null; 
        switch (position) { 
        case 0: 
            fragment=new IndexFragment(); 
            break; 
 
        default: 
            break; 
        } 
        if(fragment!=null){ 
            Activity activity=getActivity(); 
            if(activity instanceof MainActivity){ 
                ((MainActivity) activity).switchMenuFragment(fragment); 
            } 
        } 
    } 
}

 

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5412.html

(0)
上一篇 2021年7月16日
下一篇 2021年7月16日

相关推荐

发表回复

登录后才能评论