频道选择

public class ChannelActivity extends BaseActivity implements View.OnClickListener {

    private ChannelDao dao;

    private TextView txtFinish;
    private MyGridView mgvMyChannel;
    private MyGridView mgvRecommondChannel;

    private List<Channel> myChannels;
    private List<Channel> recommondChannels;

    private MyChannelAdapter myAdapter;
    private RecommondChannelAdapter recommondAdapter;

    // 是否是编辑状态
    private boolean isEditable = false;

    @Override
    protected int getContentView() {
        return R.layout.activity_channel;
    }

    @Override
    protected void initView() {
        txtFinish = findViewById(R.id.txt_finish);
        mgvMyChannel = findViewById(R.id.mgv_my_channel);
        mgvRecommondChannel = findViewById(R.id.mgv_recommond_channel);
    }

    @Override
    protected void initData() {
        super.initData();
        myChannels = new ArrayList<>();
        recommondChannels = new ArrayList<>();
        dao = new ChannelDao(this);
        addData();

        myAdapter = new MyChannelAdapter(this, myChannels);
        recommondAdapter = new RecommondChannelAdapter(this, recommondChannels);

        myAdapter.setOnItemDeleteClickListener(new MyChannelAdapter.OnItemDeleteClickListener() {
            @Override
            public void onDeleteClick(int position) {
                Channel channel = myChannels.get(position);
                // 点击删除之后应该把类型改成推荐频道
                channel.setType(2);
                recommondChannels.add(channel);
                myChannels.remove(position);

                myAdapter.notifyDataSetChanged();
                recommondAdapter.notifyDataSetChanged();

                dao.update(channel);
            }
        });

        mgvMyChannel.setAdapter(myAdapter);
        mgvRecommondChannel.setAdapter(recommondAdapter);
    }

    @Override
    protected void setListener() {
        super.setListener();
        txtFinish.setOnClickListener(this);

        mgvMyChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("index", position);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

        mgvRecommondChannel.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Channel channel = recommondChannels.get(position);
                // 点击之后添加到我的频道中,type改成1
                channel.setType(1);

                myChannels.add(channel);
                recommondChannels.remove(position);

                myAdapter.notifyDataSetChanged();
                recommondAdapter.notifyDataSetChanged();
                dao.update(channel);
            }
        });
    }

    private void addData() {
        List<Channel> channels = dao.queryAll();
        for (Channel channel : channels) {
            // 如果type为1,添加到我的频道中
            // 否则添加到推荐频道中
            if (channel.getType() == 1) {
                myChannels.add(channel);
            } else {
                recommondChannels.add(channel);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_finish:
                isEditable = !isEditable;
                myAdapter.setEditable(isEditable);
                if (isEditable) {
                    txtFinish.setText("完成");
                } else {
                    txtFinish.setText("编辑");
                }
                break;
        }
    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#8c5d5d"
        android:padding="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="排序" />

        <TextView
            android:id="@+id/txt_finish"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="编辑" />

    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="我的频道" />

            <MyGridView
                android:id="@+id/mgv_my_channel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:horizontalSpacing="5dp"
                android:numColumns="4"
                android:verticalSpacing="5dp">

            </MyGridView>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="点击添加栏目" />

            <MyGridView
                android:id="@+id/mgv_recommond_channel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:horizontalSpacing="5dp"
                android:numColumns="4"
                android:verticalSpacing="5dp">

            </MyGridView>

        </LinearLayout>

    </ScrollView>
</LinearLayout>

自定义GridView

public class MyGridView extends GridView {
    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, height);
    }
}

猜你喜欢

转载自blog.csdn.net/Lsy53122/article/details/82831566