快速实现网易新闻框架搭建

今天给大家带来的是网易新闻框架的搭建

主要用到的知识点还是drawerlayout,viewpager,tablayout,fragment。。。。

直接进入主题,上代码,快速实现:

一:进入主页面的一个activity,我们在这里面设置一个左右侧拉


1.activity_main  布局:
注意:外面的大布局换成drawerlayout:


    

    

    


2.实现MainActivity的逻辑代码:

import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout drawer_layout;
    private ActionBarDrawerToggle toggle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initView();
        init_actionBar_Draw();


    }

    //初始化控件drawerlayout实现侧拉
    private void initView() {
        drawer_layout = (DrawerLayout) findViewById(R.id.activity_main);
    }


    /**
     * 初始化ActionBar 和 DrawerLayout
     */
    private void init_actionBar_Draw() {
        //得到actionbar对象
        ActionBar actionBar = getSupportActionBar();
        //决定左上角的图标是否可以点击
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        //改变android.R.id.home返回图标。
        //Drawer拉出、隐藏,带有android.R.id.home动画效果。
        // 监听Drawer拉出、隐藏;
        toggle = new ActionBarDrawerToggle(this, drawer_layout, R.string.app_name, R.string.app_name);
        //将抽屉指示器的状态与链接的DrawerLayout同步其状态
        toggle.syncState();
        // ActionBar关联DrawerLayout
        drawer_layout.addDrawerListener(toggle);
    }


    //在点击选项菜单(OptionsMenu:点击menu弹出的菜单)的菜单项时
    // 即调用了onMenuItemSelected 也调用了onOptionsItemSelected


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (drawer_layout.isDrawerOpen(GravityCompat.END)) {
            drawer_layout.closeDrawer(GravityCompat.END);//关闭抽屉
            return super.onOptionsItemSelected(item);
        }
        return toggle.onOptionsItemSelected(item) | super.onOptionsItemSelected(item);
    }

二:通过上面的代码我们已经创建出来左右侧拉的布局,现在就是去实现左右侧拉的fragment

1.我们先实现一个右fragment:

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;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public class RightFragment extends Fragment{


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_right, container, false);
    }

2.实现左侧拉的fragment,因为左侧拉现在有我们的布局,需要实现fragment 的切换替换和隐藏等,所以我们先来
实现他的XML




    

相信大家也都看到了,我在布局里添加了一个listview,我们就是通过listview的条目实现fragment的切换等;
下面实现左侧拉fragment的逻辑代码 :

注意:代码里面的fragment你们只需要创建出来就好,如果自己 想要实现什么功能就添加代码就OK了
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.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public class LeftFragment extends Fragment{

    private MainFragment main_fragment;
    private SecondFragment second_fragment;
    private ThirdFragment three_fragment;
    private fourthFragment four_fragment;
    private FifthFragment five_fragment;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View inflate = inflater.inflate(R.layout.fragment_left, container, false);

        ListView list_view = inflate.findViewById(R.id.listview);

        List list_str = new ArrayList<>();
        for (int i = 1; i < 6; i++) {
            list_str.add("第"+i+"个Fragment");
        }
        list_view.setAdapter(new MyBaseAdapter(getActivity(),list_str));
        main_fragment = new MainFragment();
        second_fragment = new SecondFragment();
        three_fragment = new ThirdFragment();
        four_fragment = new fourthFragment();
        five_fragment = new FifthFragment();

        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.linear, main_fragment).commit();
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.linear, second_fragment).commit();
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.linear, three_fragment).commit();
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.linear, four_fragment).commit();
        getActivity().getSupportFragmentManager().beginTransaction().add(R.id.linear, five_fragment).commit();
        getActivity().getSupportFragmentManager().beginTransaction().hide(second_fragment).hide(three_fragment).hide(four_fragment).hide(five_fragment).commit();

        list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                switch (i) {
                    case 0:
                        getActivity().getSupportFragmentManager().beginTransaction().show(main_fragment).hide(second_fragment).hide(three_fragment).hide(four_fragment).hide(five_fragment).commit();
                        break;
                    case 1:
                        getActivity().getSupportFragmentManager().beginTransaction().show(second_fragment).hide(main_fragment).hide(three_fragment).hide(four_fragment).hide(five_fragment).commit();
                        break;
                    case 2:
                        getActivity().getSupportFragmentManager().beginTransaction().show(three_fragment).hide(main_fragment).hide(second_fragment).hide(four_fragment).hide(five_fragment).commit();
                        break;
                    case 3:
                        getActivity().getSupportFragmentManager().beginTransaction().show(four_fragment).hide(main_fragment).hide(second_fragment).hide(three_fragment).hide(five_fragment).commit();
                        break;
                    case 4:
                        getActivity().getSupportFragmentManager().beginTransaction().show(five_fragment).hide(main_fragment).hide(second_fragment).hide(three_fragment).hide(four_fragment).commit();
                        break;
                    default:
                        break;
                }
            }
        });

        return inflate;
    }

3.上面我们也说到了我们用到了listview,那么我们就要实现他的优化:

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public class MyBaseAdapter extends BaseAdapter {


    public Context context;
    public List list;

    public MyBaseAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View inflate = View.inflate(context, R.layout.item, null);
        TextView text = inflate.findViewById(R.id.tv_item);
        text.setText(list.get(i));
        return inflate;
    }

三:因为我们做的是网易新闻客户端的搭建,所以我需要在刚刚创建的左侧拉的fragment里面创建的第一个fragment
这就是fragment套fragment,所以我现在要在第一个创建我的逻辑代码,那么接下来就要用到我们的drawerlayout
tablayout的知识点:

1.先看XML布局:我们的tablayout是需要添加依赖的
注意:我们的依赖是:
compile 'com.android.support:design:26.0.0-alpha1'


    
    
    
    

2.看MainFragment的代码:


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/31 0031.
 */
public class MainFragment extends Fragment{

    private View inflate;
    private TabLayout tab_layout;
    private ViewPager pager;
    List list_str = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        inflate = inflater.inflate(R.layout.fragment_main, container, false);
        for (int i = 0; i < 10; i++) {
            list_str.add("No."+i);
        }
        initView();
        init_tab_pager();

        return inflate;
    }
    private void initView() {
        tab_layout = (TabLayout) inflate.findViewById(R.id.tab_layout);
        pager = (ViewPager) inflate.findViewById(R.id.pager);
    }
    private void init_tab_pager() {
        List list_fragment = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            PagerFragment fragment = new PagerFragment();
            list_fragment.add(fragment);
        }

        tab_layout.setTabMode(TabLayout.MODE_FIXED);

        for (int i = 0; i < 10; i++) {
            tab_layout.addTab(tab_layout.newTab().setText(list_str.get(i)));
        }

        MyPagerAdapter adapter = new MyPagerAdapter(getActivity().getSupportFragmentManager(),list_str,list_fragment);
        pager.setOffscreenPageLimit(1);
        pager.setAdapter(adapter);
        tab_layout.setupWithViewPager(pager);
    }

3.我们在里面创建了用到了fragment的集合,所以我们应该给他优化一下,用到fragmentpageradapter

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/8/31 0031.
 */
public class MyPagerAdapter extends FragmentPagerAdapter{



    public List list_str;
    public List list_frag;

    //接收传过来的值
    public MyPagerAdapter(FragmentManager fm, List list_str, List list_frag) {
        super(fm);
        this.list_str = list_str;
        this.list_frag = list_frag;
    }
    //返回对应位置的Fragment
    @Override
    public Fragment getItem(int position) {
        return list_frag.get(position);
    }


    @Override
    public int getCount() {
        return list_frag.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return list_str.get(position%list_str.size());
    }

4.那么我们最后在实现一下我们里面创建的pagerfragment,那么我并没有实现具体的逻辑代码,我们做的是新闻客户端,那么我们就可以在这里面实现他的加载数据的一些操作:

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;

/**
 * Created by Administrator on 2017/8/31 0031.
 */
public class PagerFragment extends Fragment{


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


//我们在这里面进行添加数据的操作
        return inflater.inflate(R.layout.fragment_pager, container, false);

    }

那么做完这些呢,我们的一个基本框架就搭建完成了,希望老铁们多多支持!!!





你可能感兴趣的:(快速实现网易新闻框架搭建)