android进阶AndroidAndroid之界面

RecyclerView仿头条新闻频道管理

2018-07-10  本文已影响385人  FlyClound

1,效果图

头条频道管理.gif

2,实现

<?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:paddingTop="15dp">

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

        <TextView
            android:id="@+id/tv_toutiao_one_my"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点击进入频道"
            android:layout_marginLeft="90dp"/>
        <TextView
            android:id="@+id/tv_toutiao_one_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"
            android:textColor="@color/red"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_toutioa_one_my"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="15dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其他频道"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点击添加频道"
            android:layout_marginLeft="90dp"/>

    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_toutioa_one_add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:overScrollMode="never"/>


</LinearLayout>
public class TouTiaoTwoActivity extends AppCompatActivity {
    private android.widget.TextView tvtoutiaoonemy;
    private android.widget.TextView tvtoutiaooneedit;
    private android.support.v7.widget.RecyclerView rvtoutioaonemy;
    private android.support.v7.widget.RecyclerView rvtoutioaoneadd;
    private ArrayList<String> mDataOne;
    private ArrayList<String> mDataTwo;
    private TouTiaoTwoMyAdapter mTouTiaoTwoMyAdapter;
    private TouTiaoTwoAdapter mTouTiaoTwoAdapter;

    boolean isEdit = false;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toutioa_one);
        this.rvtoutioaoneadd = (RecyclerView) findViewById(R.id.rv_toutioa_one_add);
        this.rvtoutioaonemy = (RecyclerView) findViewById(R.id.rv_toutioa_one_my);
        this.tvtoutiaooneedit = (TextView) findViewById(R.id.tv_toutiao_one_edit);
        this.tvtoutiaoonemy = (TextView) findViewById(R.id.tv_toutiao_one_my);

        //初始化第一个列表
        initOneList();
        //初始化第二个列表
        initTwoList();

        //编辑点击
        tvtoutiaooneedit.setOnClickListener(view -> {
            if(isEdit){
                //处于编辑状态,点击变成正常状态
                tvtoutiaooneedit.setText("编辑");
                tvtoutiaoonemy.setText("点击进入频道");
                //我的频道右上角图标隐藏
                mTouTiaoTwoMyAdapter.showDeleteIcon(false);
                //禁用拖拽
                //mTouTiaoOneAdapter.disableDragItem();
            }else {
                //点击变成编辑状态
                tvtoutiaooneedit.setText("完成");
                tvtoutiaoonemy.setText("拖拽可以排序");
                //我的频道右上角图标显示
                mTouTiaoTwoMyAdapter.showDeleteIcon(true);
                // 开启拖拽
                //mTouTiaoOneAdapter.enableDragItem(itemTouchHelper);
            }
            isEdit=!isEdit;
        });

    }
    private void initTwoList() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        mDataTwo = new ArrayList<>();
        mDataTwo.add("本地");
        mDataTwo.add("房产");
        mDataTwo.add("直播");
        mDataTwo.add("时尚");
        mDataTwo.add("小说");
        mDataTwo.add("历史");
        mDataTwo.add("育儿");
        mDataTwo.add("搞笑");
        mDataTwo.add("美食");
        mDataTwo.add("养生");
        mDataTwo.add("电影");
        mDataTwo.add("手机");
        mDataTwo.add("旅游");
        rvtoutioaoneadd.setLayoutManager(gridLayoutManager);

        mTouTiaoTwoAdapter = new TouTiaoTwoAdapter(R.layout.item_toutiao_two, mDataTwo);
        rvtoutioaoneadd.setAdapter(mTouTiaoTwoAdapter);
        //设置添加,移除动画
        rvtoutioaoneadd.setItemAnimator(new DefaultItemAnimator());
        mTouTiaoTwoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                String title = mDataTwo.get(position);
                Log.e("MMM", "onItemClick: "+position+"||"+title );
                //加入到我的频道
                mDataOne.add(title);
                mTouTiaoTwoMyAdapter.notifyItemChanged(mDataOne.size()-1);
                //mTouTiaoTwoAdapter.notifyItemInserted(mDataOne.size()-1);
//
                mDataTwo.remove(position);
                mTouTiaoTwoAdapter.notifyItemRemoved(position);
                //mTouTiaoTwoAdapter.notifyDataSetChanged();
            }
        });
    }
    private void initOneList() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        mDataOne = new ArrayList<>();
        mDataOne.add("关注");
        mDataOne.add("推荐");
        mDataOne.add("热点");
        mDataOne.add("世界杯");
        mDataOne.add("军事");
        mDataOne.add("国际");
        mDataOne.add("问答");
        mDataOne.add("视频");
        mDataOne.add("图片");
        mDataOne.add("娱乐");
        mDataOne.add("科技");
        mDataOne.add("国风");
        mDataOne.add("地理");
        mDataOne.add("地球仪");
        rvtoutioaonemy.setLayoutManager(gridLayoutManager);

        mTouTiaoTwoMyAdapter = new TouTiaoTwoMyAdapter(this, mDataOne, new TouTiaoTwoMyAdapter.AdapterCallBack() {
            @Override
            public void onItemClickListener(TouTiaoTwoMyAdapter.ViewHolder viewHolder, int pos) {
                //点击监听
                //处于编辑状态
                if (isEdit){
                    //点击移除,前两个除外
                    if (pos>1){
                        //加入频道推荐,//将其移动到第一个位置
                        String title = mDataOne.get(pos);
                        //Log.e(TAG, "onItem--my--Click: "+position+"||"+title );
                        mDataTwo.add(0,title);
                        mTouTiaoTwoAdapter.notifyItemInserted(0);
                        //mTouTiaoTwoAdapter.notifyDataSetChanged();
                        //我的频道,移除点击的数据
                        mDataOne.remove(pos);
                        mTouTiaoTwoMyAdapter.notifyItemRemoved(pos);
                        //刷新数据
                        mTouTiaoTwoMyAdapter.notifyItemChanged(pos,mDataOne.size()-pos);
                        //mTouTiaoOneAdapter.notifyDataSetChanged();
                    }
                }else {
                    //点击进入对应详情页
                    String title = mDataOne.get(pos);
                    Intent intent = new Intent(TouTiaoTwoActivity.this, TouTiaoDetailActivity.class);
                    intent.putExtra("title",title);
                    startActivity(intent);
                }
            }

            @Override
            public boolean onItemLongClickListener(TouTiaoTwoMyAdapter.ViewHolder viewHolder, int pos) {
                if (isEdit){
                    return false;
                }
                if (pos > 1){
                    //长按处于编辑状态
                    isEdit=true;
                    //点击变成编辑状态
                    tvtoutiaooneedit.setText("完成");
                    tvtoutiaoonemy.setText("拖拽可以排序");
                    //我的频道右上角图标显示
                    mTouTiaoTwoMyAdapter.showDeleteIcon(true);
                    return true;
                }
                return false;
            }
        });

        //自定义类实现拖拽,侧滑删除
        MyItemTouchHandler myItemTouchHandler = new MyItemTouchHandler(mTouTiaoTwoMyAdapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new MyItemTouchHandler(mTouTiaoTwoMyAdapter));
        itemTouchHelper.attachToRecyclerView(rvtoutioaonemy);

        rvtoutioaonemy.setAdapter(mTouTiaoTwoMyAdapter);
        //设置添加,移除动画
        rvtoutioaonemy.setItemAnimator(new DefaultItemAnimator());
    }
}

public class TouTiaoTwoMyAdapter extends MyItemTouchHandler.ItemTouchAdapterImpl {


    private final LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<String>mData;
    AdapterCallBack mAdapterCallBack;

    public TouTiaoTwoMyAdapter(Context context, List<String> data,AdapterCallBack adapterCallBack) {
        mContext = context;
        mData = data;
        this.mAdapterCallBack=adapterCallBack;
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {

    }

    /**禁止自动拖拽
     * @return
     */
//    @Override
//    protected boolean autoOpenDrag() {
//        return false;
//    }



    /**禁止滑动删除
     * @return
     */
    @Override
    protected boolean autoOpenSwipe() {
        return false;
    }

    @Override
    public void onItemRemove(int position) {

    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_toutiao_one, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,
                                 @SuppressLint("RecyclerView") int position) {
        if (holder instanceof ViewHolder) {
            ((ViewHolder) holder).tvTitle.setText(mData.get(position));

                if (isDelete){
                    if (position > 1) {
                        ((ViewHolder) holder).ivDelete.setVisibility(View.VISIBLE);
                    }
                }else {
                    ((ViewHolder) holder).ivDelete.setVisibility(View.GONE);
                }

            holder.itemView.setOnClickListener(view -> {
                //点击监听
                mAdapterCallBack.onItemClickListener((ViewHolder) holder,position);
            });

//            //长按点击监听
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    mAdapterCallBack.onItemLongClickListener((ViewHolder) holder,position);
                    return false;
                }
            });

        }
    }
    private boolean isDelete;
    public void showDeleteIcon(boolean isDelete){
        this.isDelete = isDelete;
        notifyDataSetChanged();
    }

    public interface AdapterCallBack{
        void onItemClickListener(ViewHolder viewHolder,int pos);
        boolean onItemLongClickListener(ViewHolder viewHolder,int pos);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        ImageView ivDelete;
        public ViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_item_toutiao_one_title);
            ivDelete = itemView.findViewById(R.id.iv_item_toutiao_one_delete);
        }
    }
}
public class TouTiaoTwoAdapter extends BaseQuickAdapter<String,BaseViewHolder> {
    public TouTiaoTwoAdapter(int layoutResId, @Nullable List<String> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, String item) {
        helper.setText(R.id.tv_item_toutiao_two_title,item);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp">

    <TextView
        android:id="@+id/tv_item_toutiao_one_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="标题"
        android:layout_margin="5dp"
        android:gravity="center"
        android:background="#f0f0f0"
        />

    <ImageView
        android:id="@+id/iv_item_toutiao_one_delete"
        android:layout_width="10dp"
        android:layout_height="10dp"
        android:src="@color/red"
        android:layout_alignParentRight="true"
        android:visibility="gone"/>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="5dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp">

    <TextView
        android:id="@+id/tv_item_toutiao_two_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="标题"
        android:layout_margin="5dp"
        android:gravity="center"
        android:background="#f0f0f0"
        />


</RelativeLayout>
上一篇下一篇

猜你喜欢

热点阅读