DragGridDemo 频道管理器的使用(demo)

来源:互联网 发布:epson机器人编程 编辑:程序博客网 时间:2024/06/01 08:22

在项目的build.gradle下面 添加

allprojects {    repositories {        jcenter()        maven {url "https://jitpack.io"}//*****    }}

并且添加依赖

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



xml文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.bawei.pin.MainActivity">  <android.support.design.widget.TabLayout      android:layout_width="match_parent"      android:layout_height="wrap_content"      app:tabMode="scrollable"      android:id="@+id/tab"/>    <ImageButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:src="@drawable/ems_pulltorefresh_down_arrow"        android:id="@+id/btn"/></RelativeLayout>



java 代码


package com.bwie.draggriddemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.andy.library.ChannelActivity;
import com.andy.library.ChannelBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TabLayout mMytab;
    private ImageButton mImgBtn;
    private ArrayList<ChannelBean> channelBeens;
    String jsonStr = "";
    private Gson gson;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }
    private void initData(){
        //准备栏目数据
        channelBeens = new ArrayList<ChannelBean>();
        channelBeens.add(new ChannelBean("热点",true));
        channelBeens.add(new ChannelBean("军事",true));
        channelBeens.add(new ChannelBean("八卦",true));
        channelBeens.add(new ChannelBean("游戏",true));
        channelBeens.add(new ChannelBean("宠物",true));
        channelBeens.add(new ChannelBean("汽车",true));
        channelBeens.add(new ChannelBean("热卖",true));
        channelBeens.add(new ChannelBean("外卖",true));
        channelBeens.add(new ChannelBean("条目1",true));
        channelBeens.add(new ChannelBean("条目2",true));
        channelBeens.add(new ChannelBean("条目3",false));
        channelBeens.add(new ChannelBean("条目4",false));
        channelBeens.add(new ChannelBean("条目5",false));
        channelBeens.add(new ChannelBean("条目6",false));
        channelBeens.add(new ChannelBean("条目7",false));
        channelBeens.add(new ChannelBean("条目8",false));
        //把选择的栏目(true)数据配置给tablayout
        for (int i=0;i<channelBeens.size();i++){
            if(channelBeens.get(i).isSelect()){
                mMytab.addTab(mMytab.newTab().setText(channelBeens.get(i).getName()));
            }
        }
    }
    private void initView() {
        mMytab = (TabLayout) findViewById(R.id.mytab);
        mImgBtn = (ImageButton) findViewById(R.id.imgBtn);
        mImgBtn.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.imgBtn:
                ChannelActivity.startChannelActivity(MainActivity.this,channelBeens);
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == ChannelActivity.REQUEST_CODE && resultCode == ChannelActivity.RESULT_CODE){//为true表示是频道管理回调回来的
            jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);//得到栏目管理的结果
            Toast.makeText(this,jsonStr,Toast.LENGTH_SHORT).show();
            Log.i("main",jsonStr);
            mMytab.removeAllTabs();//清空之前的栏目
            //把新选择的栏目结果更新到tablayout上
            gson = new Gson();
            //进行json解析
            Type type= new TypeToken<ArrayList<ChannelBean>>(){}.getType();
            channelBeens = gson.fromJson(jsonStr,type);
            //遍历结果,更新tablayout
            for (int i=0;i<channelBeens.size();i++){
                if(channelBeens.get(i).isSelect()){
                    mMytab.addTab(mMytab.newTab().setText(channelBeens.get(i).getName()));
                }
            }
        }
    }
}




阅读全文
0 0
原创粉丝点击