大佬教程收集整理的这篇文章主要介绍了android – 单击AlertDialog项目列表时在RecyclerView布局之间切换,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在onCreateViewHolder方法中切换视图,以便用户从AlertDialog列表中选择
这个在MainActivity中的onOptionsItemSELEcted其中有菜单项“更改布局”,以便当用户按下它时出现AlertDialog列表
@Override public Boolean onOptionsItemSELEcted(MenuItem item) { if (item.getItemId() == R.id.change_layout) { AlertDialog.builder builder = new AlertDialog.builder(this); builder.settitle(getString(R.String.choose_layout)); String[] layouts = {"title Layout","Cards List","Card Magazine Layout"}; builder.setItems(layouts,new DialogInterface.onClickListener() { @Override public void onClick(DialogInterface dialog,int indeX) { switch (indeX) { case 0: // title layout Toast.makeText(MainActivity.this,"title layout",Toast.LENGTH_LONG).show(); break; case 1: // Cards List Toast.makeText(MainActivity.this,"Card list",Toast.LENGTH_LONG).show(); break; case 2: // Cards Magazine Layout Toast.makeText(MainActivity.this,"Card Magazine Layout",Toast.LENGTH_LONG).show(); } } });
这是我的自定义适配器类PostAdapter
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> { private Context context; private List<Item> items; public PostAdapter(Context context,List<Item> items) { this.context = context; this.items = items; } @NonNull @Override public PostAdapter.PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewTypE) { LayoutInflater inflater = LayoutInflater.from(context); // here's the three layouts that I can't switch between it View cardsListLayout = inflater.inflate(R.layout.post_item_card_layout,parent,falsE); View titleLayout = inflater.inflate(R.layout.post_item_grid_layout,falsE); View cardMagazineLayout = inflater.inflate(R.layout.card_magazine_layout,falsE); return new PostViewHolder(titleLayout); } @Override public void onBindViewHolder(@NonNull PostViewHolder holder,int position) { final Item item = items.get(position); holder.posttitle.setText(item.gettitle()); final Document document = Jsoup.parse(item.getContent()); Elements elements = document.SELEct("img"); Log.e("CODE","Image: " + elements.get(0).attr("src")); // Log.d("Text",document.text()); // holder.postDescription.setText(document.text()); Glide.with(context).load(elements.get(0).attr("src")) .into(holder.postImagE); holder.itemView.setOnClickListener(new View.onClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(context,DetailsActivity.class); intent.putExtra("url",item.getUrl()); intent.putExtra("title",item.gettitle()); intent.putExtra("content",item.getContent()); int youtubeThumbnailImagesetVisibility = 0; Element element = document.body(); String youtubeThumbnailImageSrc = ""; String youTubeLink = ""; for (Element e : element.getElementsByClass ("YOUTUBE-iframe-video")) { youtubeThumbnailImageSrc = e.attr("data-thumbnail-src"); youTubeLink = e.attr("src"); Log.e("YouTube thumbnail",youtubeThumbnailImageSrc); Log.e("Youtube link",youTubeLink); } if (youtubeThumbnailImageSrc.isEmpty()) { youtubeThumbnailImagesetVisibility = 8; intent.putExtra("youtubeThumbnailImagesetVisibility",youtubeThumbnailImagesetVisibility); } else { intent.putExtra("youtubeThumbnailImageSrc",youtubeThumbnailImageSrc); intent.putExtra("youTubeLink",youTubeLink); } // String imageSrc = elements.get(0).attr("src"); // intent.putExtra("blogImage",imageSrc); context.startActivity(intent); } }); } @Override public int getItemCount() { return items.size(); } public class PostViewHolder extends RecyclerView.ViewHolder { ImageView postImage; TextView posttitle; TextView postDescription; public PostViewHolder(View itemView) { super(itemView); postImage = itemView.findViewById(R.id.postImagE); posttitle = itemView.findViewById(R.id.posttitlE); postDescription = itemView.findViewById(R.id.postDescription); } } }
我为每一个创建了两个不同的布局管理器,有一个linearlayoutmanager和GridLayoutManager,目前我使用了GridLayout,所以我暂时评论了LinearLayoutmanger,直到我知道如何在它们之间切换
// linearlayoutmanager = new linearlayoutmanager(this); gridLayoutManager = new GridLayoutManager(this,2,RecyclerView.VERTICAL,falsE); // recyclerView.setLayoutManager(linearlayoutmanager); recyclerView.setLayoutManager(gridLayoutManager);
我渴望的结果
您可以使用具有多个viewType的单个Recyclerview实现此目的
当您想要更改Recyclerview的布局时,只需更改Recyclerview的LayoutManager和viewType即可
示例代码
试试这种方式
首先为多个viewType创建三个布局
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://scheR_768_11845@as.android.com/apk/res/android" xmlns:app="http://scheR_768_11845@as.android.com/apk/res-auto" android:layout_width="150dp" android:layout_height="150dp" app:cardElevation="10dp" app:cardUseCompatPadding="true"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="150dp" android:adjustViewBounds="true" android:contentDescription="@String/app_name" android:scaleType="centerCrop" android:src="@drawable/dishu" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:BACkground="#6a000000" android:gravity="center" android:padding="10dp" android:text="dummy text" android:textColor="@android:color/white" /> </RelativeLayout> </android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://scheR_768_11845@as.android.com/apk/res/android" xmlns:app="http://scheR_768_11845@as.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="10dp" app:cardUseCompatPadding="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="150dp" android:adjustViewBounds="true" android:contentDescription="@String/app_name" android:scaleType="centerCrop" android:src="@drawable/dishu" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingStart="5dp" android:paddingend="10dp" android:text="Dummy title" android:textColor="@android:color/black" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingStart="5dp" android:paddingend="10dp" android:text="Dummy Tex" /> </LinearLayout> </android.support.v7.widget.CardView>
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://scheR_768_11845@as.android.com/apk/res/android" xmlns:app="http://scheR_768_11845@as.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardElevation="10dp" app:cardUseCompatPadding="true"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingStart="5dp" android:paddingend="5dp" android:text="dummy text" android:textColor="@android:color/black" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="start" android:paddingStart="5dp" android:paddingend="5dp" android:text="I have three different layouts cardsListLayout,cardMagazineLayoutand there may be more in the future,which used as a views on onCreateViewHolder method." /> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="150dp" android:adjustViewBounds="true" android:contentDescription="@String/app_name" android:scaleType="centerCrop" android:src="@drawable/dishu" /> </LinearLayout> </android.support.v7.widget.CardView>
package neel.com.recyclerviewdemo; import android.content.Context; import android.support.Annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class DataAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public static final int ITEM_TYPE_GRID = 0; public static final int ITEM_TYPE_CARD_LIST = 1; public static final int ITEM_TYPE_titlE_LIST = 2; private Context mContext; privatE int VIEW_TYPE = 0; public DataAdapter(Context mContext) { this.mContext = mContext; } public void setVIEW_TYPE(int viewTypE) { VIEW_TYPE = viewType; notifyDataSetChanged(); } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewTypE) { View view = null; // check here the viewType and return RecyclerView.ViewHolder based on view type switch (VIEW_TYPE) { case ITEM_TYPE_GRID: // if VIEW_TYPE is Grid than return GridViewHolder view = LayoutInflater.from(mContext).inflate(R.layout.grid_layout,falsE); return new GridViewHolder(view); case ITEM_TYPE_CARD_LIST: // if VIEW_TYPE is Card List than return CardListViewHolder view = LayoutInflater.from(mContext).inflate(R.layout.cardlist_layout,falsE); return new CardListViewHolder(view); case ITEM_TYPE_titlE_LIST: // if VIEW_TYPE is title List than return titleListViewHolder view = LayoutInflater.from(mContext).inflate(R.layout.title_layout,falsE); return new titleListViewHolder(view); } return new GridViewHolder(view); } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,int position) { final int itemType = getItemViewType(position); // First check here the View Type // than set data based on View Type to your recyclerview item if (itemType == ITEM_TYPE_GRID) { if (holder instanceof CardViewHolder) { GridViewHolder viewHolder = (GridViewHolder) holder; // write here code for your grid list } } else if (itemType == ITEM_TYPE_CARD_LIST) { if (holder instanceof CardViewHolder) { CardListViewHolder buttonViewHolder = (CardListViewHolder) holder; // write here code for your grid list } } else if (itemType == ITEM_TYPE_titlE_LIST) { if (holder instanceof CardViewHolder) { titleListViewHolder buttonViewHolder = (titleListViewHolder) holder; // write here code for your titleListViewHolder } } } @Override public int getItemCount() { return 40; } // RecyclerView.ViewHolder class for gridLayoutManager public class GridViewHolder extends RecyclerView.ViewHolder { public GridViewHolder(@NonNull View itemView) { super(itemView); } } // RecyclerView.ViewHolder class for Card list View public class CardListViewHolder extends RecyclerView.ViewHolder { public CardListViewHolder(@NonNull View itemView) { super(itemView); } } // RecyclerView.ViewHolder class for title list View public class titleListViewHolder extends RecyclerView.ViewHolder { public titleListViewHolder(@NonNull View itemView) { super(itemView); } } }
package neel.com.recyclerviewdemo; import android.content.DialogInterface; import android.os.bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.RecyclerView; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; public class MainActivity extends AppCompatActivity { DataAdapter dataAdapter; private RecyclerView myRecyclerView; private linearlayoutmanager linearlayoutmanager; private GridLayoutManager gridLayoutManager; @Override protected void onCreate(Bundle savedInstanceStatE) { super.onCreate(savedInstanceStatE); setContentView(R.layout.activity_main); myRecyclerView = findViewById(R.id.myRecyclerView); myRecyclerView.setHasFixedSize(true); linearlayoutmanager = new linearlayoutmanager(this); gridLayoutManager = new GridLayoutManager(this,3); myRecyclerView.setLayoutManager(gridLayoutManager); dataAdapter = new DataAdapter(this); myRecyclerView.setAdapter(dataAdapter); } @Override public Boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.home_menu,menu); // return true so that the menu pop up is opened return true; } @Override public Boolean onOptionsItemSELEcted(MenuItem item) { if (item.getItemId() == R.id.action_dialog) { AlertDialog.builder builder = new AlertDialog.builder(this); builder.settitle("Please choose a layout"); String[] layouts = {"title Layout","Grid View"}; builder.setItems(layouts,int indeX) { switch (indeX) { case 0: // title layout dataAdapter.setVIEW_TYPE(2); myRecyclerView.setLayoutManager(linearlayoutmanager); myRecyclerView.setAdapter(dataAdapter); break; case 1: // Cards List dataAdapter.setVIEW_TYPE(1); myRecyclerView.setLayoutManager(linearlayoutmanager); myRecyclerView.setAdapter(dataAdapter); break; case 2: // Grid Layout dataAdapter.setVIEW_TYPE(0); myRecyclerView.setLayoutManager(gridLayoutManager); myRecyclerView.setAdapter(dataAdapter); } } }); builder.show(); } return super.onOptionsItemSELEcted(item); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://scheR_768_11845@as.android.com/apk/res/android" xmlns:tools="http://scheR_768_11845@as.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/myRecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
OUTPUT
以上是大佬教程为你收集整理的android – 单击AlertDialog项目列表时在RecyclerView布局之间切换全部内容,希望文章能够帮你解决android – 单击AlertDialog项目列表时在RecyclerView布局之间切换所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。