先展示一下效果
一、来看一下布局代码
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:text="切换的栏目"
android:textSize="20sp" />
<GridView
android:id="@+id/G1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:padding="5dp"
android:scrollbars="none"
android:verticalSpacing="5dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:text="更多栏目"
android:textSize="20sp" />
<GridView
android:id="@+id/G2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:padding="5dp"
android:scrollbars="none"
android:verticalSpacing="5dp" />
//样式在drawble下创建xml给属性
<shape xmlns:android=“http://schemas.android.com/apk/res/android”
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#ff9900"/>
//以及在layout里创xml给Textview的父样式
<TextView
android:background="@drawable/sh"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:text="XXXXX"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
tools:ignore="MissingConstraints" />
一、来看一下MainActivity代码
public class MainActivity extends AppCompatActivity {
private GridView G1,G2;
private ArrayList<String> list1;
private ArrayList<String> list2;
private GridAdapter adapter;
private GridAdapter adapter1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
G1 = findViewById(R.id.G1);
G2 = findViewById(R.id.G2);
list1 = new ArrayList<>();
list2 = new ArrayList<>();
//模拟的list的数据
list1.add("刘老根");
list1.add("杜子藤");
list1.add("丁克");
list1.add("丛从林");
list1.add("赵大爷");
list1.add("朱八戒");
list1.add("张开推");
list1.add("宋外侩");
for (int i = 0; i<12; i++){
list2.add("你妈炸了"+i);
}
//适配器
adapter = new GridAdapter(list1, MainActivity.this);
adapter1 = new GridAdapter(list2, MainActivity.this);
G1.setAdapter(adapter);
G2.setAdapter(adapter1);
G1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//保存要删除的数据
String str = list1.get(position);
Toast.makeText(MainActivity.this,str.toString(),Toast.LENGTH_LONG).show();
//删除第一个中的数据
list1.remove(position);
//把保存的数据传到第二个中
list2.add(str);
//刷新两个适配器
adapter.notifyDataSetChanged();
adapter1.notifyDataSetChanged();
}
});
G2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//保存要删除的数据
String str = list2.get(position);
//删除第一个中的数据
list2.remove(position);
//把保存的数据传到第二个中
list1.add(str);
//刷新两个适配器
adapter.notifyDataSetChanged();
adapter1.notifyDataSetChanged();
}
});
}
}
三、适配器类
public class GridAdapter extends BaseAdapter {
private ArrayList<String> list;
private Context context;
public GridAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(context,R.layout.item,null);
TextView textView = convertView.findViewById(R.id.text1);
textView.setText(list.get(position));
Toast.makeText(context,list.get(position).toString(),Toast.LENGTH_LONG).show();
return convertView;
}
}