Android 频道管理仿今日头条

导入依赖

在项目的build.gradle导入

allprojects {
    repositories {
        jcenter()
//下面这一行就是
 maven { url "https://jitpack.io" }
    }
}

在module里的build.gradle导入

compile 'com.github.yekh:draggrid:v1.0.1'


布局写一个按钮创造点击事件就可以,下面是代码 

public class MainActivity extends AppCompatActivity {

    private List list = new ArrayList<>();
    private String jsonBeans;
    private SharedPreferences preferences;
    private Button pindao;
    private Button tab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        pindao = (Button) findViewById(R.id.pindao);
        tab = (Button) findViewById(R.id.btn_tab);
        //初始化sharedpreference
        preferences = getSharedPreferences("channel", MODE_PRIVATE);
        jsonBeans = preferences.getString("beans", null);

        //创建监听事件进入频道管理
        pindao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (jsonBeans == null) {
                    //添加频道数据
                    initList();
                    ChannelActivity.startChannelActivity(MainActivity.this, list);
                } else {
                    ChannelActivity.startChannelActivity(MainActivity.this, jsonBeans);
                }
            }
        });

    }

    private void initList() {
        list.add(new ChannelBean("热点", true));
        list.add(new ChannelBean("视频", true));
        list.add(new ChannelBean("社会", false));
        list.add(new ChannelBean("图片", false));
        list.add(new ChannelBean("军事", false));
        list.add(new ChannelBean("体育", false));
        list.add(new ChannelBean("汽车", false));
        list.add(new ChannelBean("国境", false));
        list.add(new ChannelBean("娱乐", false));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE) {
            jsonBeans = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);

        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("beans", jsonBeans);
        editor.commit();
    }
}



你可能感兴趣的:(Android,频道管理)