1. 程式人生 > >draggrid簡單用法——實現頻道管理

draggrid簡單用法——實現頻道管理

1:匯入依賴到自己工程下的gradle 

 com.github.andyoom:draggrid:v1.0.1

2.在專案的build.gradle中新增
allprojects {
    repositories {
        jcenter()
//下面這句才是加上的,上面的自帶 的
        maven {url "https://jitpack.io"}
    }
}

3.許可權
    <!-- 在SDCard中建立與刪除檔案許可權 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />


    <!-- 往SDCard寫入資料許可權 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <!-- 震動許可權 -->

    <uses-permission android:name="android.permission.VIBRATE"/>

4:在Activity下主要程式碼:

//按鈕點選跳轉到控制介面
    public void button(View view){

if (list ==null){//判斷集合中是否已有資料,沒有則建立
            list =new ArrayList<>();
            //第一個是顯示的條目,第二個引數是否顯示
            list.add(new ChannelBean("熱點",true));
            list.add(new ChannelBean("軍事",true));
            list.add(new ChannelBean("八卦",true));
            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("條目1",false));
            list.add(new ChannelBean("條目2",false));
            list.add(new ChannelBean("條目3",false));
            list.add(new ChannelBean("條目4",false));
            ChannelActivity.startChannelActivity(MainActivity.this, list);
        }else if (jsonStr!=null){//當判斷儲存的字串不為空的時候,直接載入已經有了的字串
            ChannelActivity.startChannelActivity(MainActivity.this,jsonStr);
        }
    }
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==ChannelActivity.REQUEST_CODE&&resultCode==ChannelActivity.RESULT_CODE) {


        jsonStr = data.getStringExtra(ChannelActivity.RESULT_JSON_KEY);
    } }

上方引數中顯示為true,將會在頻道管理中,我的頻道里。

顯示為false的將會在其他頻道顯示

————————————————分割線——————————————————

許可權:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission
> <uses-permission android:name="android.permission.VIBRATE"></uses-permission> <application android:allowBackup="true"
佈局:
<?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"
tools:context="com.bwie.draggriddemo.MainActivity">

    <android.support.design.widget.TabLayout
android:id="@+id/mytab"
app:tabMode="scrollable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
    <ImageButton
android:id="@+id/imgBtn"
android:src="@mipmap/add_recipients_normal"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity :
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()));
                }
            }
        }
    }
}