1. 程式人生 > >頻道管理(簡單使用但返回按鈕不能使用)轉自他人

頻道管理(簡單使用但返回按鈕不能使用)轉自他人

匯入依賴

在專案的build.gradle匯入

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

在module裡的build.gradle匯入

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

佈局寫一個按鈕創造點選事件就可以,下面是程式碼

public class MainActivity extends AppCompatActivity {
private List<ChannelBean> 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();
               preferences.edit()
                .putString( "beans",string )
                .commit();
    }
}

作者:老傘czx
來源:CSDN
原文:https://blog.csdn.net/PowerSen/article/details/78036857