两个关联的GridView,点击其中一个,另一个的状态发生变化,联动操作

来源:互联网 发布:javascript focus用法 编辑:程序博客网 时间:2024/06/10 18:15

最近做了个练习,为某个用户设置标签,标签页下方是可供选择的所有标签,上方是选中的标签,点击所有标签中的一项,上方列表显示相应的选项,点击上方列表中的一项,删除该项,下方所有标签列表中的该项呈未选中状态。如图所示:


页面代码:

package com.example.castedemo.user;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.Button;import android.widget.GridView;import android.widget.RelativeLayout;import com.example.castedemo.R;import com.example.castedemo.family.adapter.NewTagAdapter;import com.example.castedemo.family.adapter.TagAdapter;import com.example.castedemo.user.bean.TagBean;import java.util.ArrayList;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;import butterknife.OnClick;public class UserTagActivity extends Activity {    private static final String TAG = "UserTagActivity";    @BindView(R.id.gv_addedTag)    GridView gvAddedTag;    @BindView(R.id.gv_allTag)    GridView gvAllTag;    //    @BindView(R.id.iv_back)//    ImageView ivBack;//    @BindView(R.id.rl_bottom)//    RelativeLayout rlBottom;    String[] allTags = {"推荐", "热点", "视频", "图片", "段子", "社会", "娱乐", "科技"};    NewTagAdapter tagAdapter;    @BindView(R.id.btn_tagCancel)    Button btnTagCancel;    @BindView(R.id.btn_tagSave)    Button btnTagSave;    Context mContext;    List<String> selTags = new ArrayList<String>();    List<TagBean> tagBeen = new ArrayList<TagBean>();    List<TagBean> selTagBeans = new ArrayList<TagBean>();    NewTagAdapter selTagAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_user_tag);        ButterKnife.bind(this);        mContext = UserTagActivity.this;        initData();        initSelData();    }    public void initData() {        for(int i=0;i<allTags.length;i++){            TagBean bean = new TagBean();            bean.setTag(allTags[i]);            bean.setSel(false);            tagBeen.add(bean);        }        tagAdapter = new NewTagAdapter(mContext,tagBeen);        gvAllTag.setAdapter(tagAdapter);        gvAllTag.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                TagBean tagBean = tagBeen.get(position);                Log.e(TAG,"tagName="+tagBean.getTag()+","+tagBean.isSel());                if(tagBean.isSel()){                    tagBean.setSel(false);                    selTagBeans.remove(tagBean);                }else{                    tagBean.setSel(true);                    selTagBeans.add(tagBean);                }                tagAdapter.notifyDataSetChanged();                selTagAdapter.notifyDataSetChanged();            }        });    }    public void initSelData(){        if(getIntent().getStringExtra("selTag") != null){            String tags = getIntent().getStringExtra("selTag");            String[] saveTag = tags.split(",");
//将选中的标签列表设置成与所有标签相同的数据结构            for(int i=0;i<saveTag.length;i++){                for(int j=0;j<tagBeen.size();j++){                    TagBean bean = tagBeen.get(j);                    if(saveTag[i].equals(bean.getTag())){                        bean.setSel(true);                        selTagBeans.add(bean);                    }                }            }        }        selTagAdapter = new NewTagAdapter(mContext,selTagBeans);        gvAddedTag.setAdapter(selTagAdapter);        gvAddedTag.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                TagBean bean = selTagBeans.get(position);                bean.setSel(false);                selTagBeans.remove(position);                selTagAdapter.notifyDataSetChanged();                tagAdapter.notifyDataSetChanged();            }        });    }    @OnClick({R.id.btn_tagCancel, R.id.btn_tagSave})    public void onClick(View view) {        switch (view.getId()) {            case R.id.btn_tagCancel:                finish();                break;            case R.id.btn_tagSave:                String passTags = "";                for(int i=0;i<selTagBeans.size();i++){//                    passTags[i] = selTagBeans.get(i).getTag();                    if(selTagBeans.size()-1 == i){                        passTags += selTagBeans.get(i).getTag();                    }else{                        passTags += selTagBeans.get(i).getTag()+",";                    }                }                Intent userIntent = new Intent();                userIntent.putExtra("tag",passTags);                setResult(RESULT_OK,userIntent);                finish();                break;        }    }}
适配器代码:
package com.example.castedemo.family.adapter;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import com.example.castedemo.R;import com.example.castedemo.user.bean.TagBean;import java.util.List;import butterknife.BindView;import butterknife.ButterKnife;/** * Created by wangchm on 2017/10/26 0026. * 偏好标签页 */public class NewTagAdapter extends BaseAdapter {    private static final String TAG = "TagAdapter";    private Context mContext;    private List<TagBean> tagBeen;    public NewTagAdapter(Context mContext, List<TagBean> tagBeen) {        this.mContext = mContext;        this.tagBeen = tagBeen;    }    @Override    public int getCount() {        return tagBeen == null ? 0 : tagBeen.size();    }    @Override    public Object getItem(int position) {        return position;    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ViewHolder holder;        if (convertView == null) {            convertView = LayoutInflater.from(mContext).inflate(R.layout.tag_item, null);            holder = new ViewHolder(convertView);            convertView.setTag(holder);        }else{            holder = (ViewHolder) convertView.getTag();        }        TagBean bean = tagBeen.get(position);        if(bean.isSel()){            holder.ivCheck.setVisibility(View.VISIBLE);        }else{            holder.ivCheck.setVisibility(View.GONE);        }        holder.tvTag.setText(bean.getTag());        return convertView;    }    static class ViewHolder {        @BindView(R.id.tv_tag)        TextView tvTag;        @BindView(R.id.iv_tagCheck)        ImageView ivCheck;        ViewHolder(View view) {            ButterKnife.bind(this, view);        }    }}

子项布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv_tag"        android:padding="10dp"        android:text="推荐"        android:layout_marginTop="5dp"        android:layout_marginRight="5dp"        android:background="@drawable/border_gray_style"        android:textColor="@color/text_white"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <ImageView        android:id="@+id/iv_tagCheck"        android:visibility="gone"        android:layout_alignRight="@+id/tv_tag"        android:layout_alignTop="@+id/tv_tag"        android:layout_marginTop="-5dp"        android:layout_marginRight="-5dp"        android:src="@drawable/delete_check"        android:layout_width="18dp"        android:layout_height="18dp" /></RelativeLayout>

阅读全文
0 0