我想点击每个'图片视图'时更改视图数组
当我点击1号'imageview'加载1列网格视图
点击no.2'image' - > 2列网格视图3号'imageview' - > 3列网格视图
我为每个网格视图制作了xml文件。我该怎么做?
public class MainActivity extends AppCompatActivity {
ImageView menu1,menu2,menu3;
GridView first,second,third;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu1 = (ImageView) findViewById(Nemo1);
menu2 = (ImageView) findViewById(R.id.Nemo2);
menu3 = (ImageView) findViewById(R.id.Nemo3);
first = (GridView) findViewById(R.id.grid1);
second = (GridView) findViewById(R.id.grid2);
third = (GridView) findViewById(R.id.grid3);
menu1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
first.setVisibility(View.VISIBLE);
v.setVisibility(View.GONE);
}
});
menu2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
second.setVisibility(View.VISIBLE);
v.setVisibility(View.GONE);
}
});
menu3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
third.setVisibility(View.VISIBLE);
v.setVisibility(View.GONE);
}
});
}
}
答案 0 :(得分:0)
•MainActivity.java
public class MainActivity extends AppCompatActivity {
private RecyclerAdapter recyclerAdapter;
private ImageView menu1,menu2,menu3;
private RecyclerView recycler;
private GridLayoutManager gridLayoutManager;
private ArrayList<String> list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler = (RecyclerView) findViewById(R.id.recycler);
list = new ArrayList<String>();
menu1 = (ImageView) findViewById(R.id.Nemo1);
menu2 = (ImageView) findViewById(R.id.Nemo2);
menu3 = (ImageView) findViewById(R.id.Nemo3);
for(int i = 0; i < 10 ; i++){
list.add("item " +i);
}
recycler.setHasFixedSize(true);
recyclerAdapter = new RecyclerAdapter(MainActivity.this, list);
recycler.setAdapter(recyclerAdapter);
menu1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
gridLayoutManager = new GridLayoutManager(MainActivity.this, 1);
recycler.setLayoutManager(gridLayoutManager);
}
});
menu2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
recycler.setLayoutManager(gridLayoutManager);
}
});
menu3.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
gridLayoutManager = new GridLayoutManager(MainActivity.this, 3);
recycler.setLayoutManager(gridLayoutManager);
}
});
}
}
•RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private Context mContext;
private ArrayList<String> list;
public RecyclerAdapter(Context mContext, ArrayList<String> list) {
this.mContext = mContext;
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemTextView.setText(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView itemTextView;
public ViewHolder(View itemView) {
super(itemView);
itemTextView = (TextView) itemView.findViewById(R.id.itemTextView);
}
}
}
•item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/itemTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="14sp"
android:textColor="@color/colorPrimary"
android:text="item1"/>
</LinearLayout>
•activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="pithiyasagar.gridviewcolumn.MainActivity">
<LinearLayout
android:id="@+id/lnrImageButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/Nemo1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
android:layout_weight="1"/>
<ImageView
android:id="@+id/Nemo2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
android:layout_weight="1"/>
<ImageView
android:id="@+id/Nemo3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@mipmap/ic_launcher"
android:layout_weight="1"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/lnrImageButton">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
•输出屏幕: