android– 单击AlertDialog项目列表时在RecyclerView布局之间切换

android– 单击AlertDialog项目列表时在RecyclerView布局之间切换,第1张

概述我有三种不同的布局cardListLayout,titleLayout,cardMagazineLayout,将来可能会有更多,用作onCreateViewHolder方法的视图.我想在onCreateViewHolder方法中切换视图,以便用户从AlertDialog列表中选择这个在MainActivity中的onOptionsItemSelected其中有菜单项“更改布局”,以便当

我有三种不同的布局cardListLayout,TitleLayout,cardMagazineLayout,将来可能会有更多,用作onCreateVIEwHolder方法的视图.

我想在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, parent, false);        VIEw cardMagazineLayout = inflater.inflate(R.layout.card_magazine_layout,parent,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);

我渴望的结果

解决方法:

无需为此使用多个RecyclervIEw

您可以使用具有多个vIEwType的单个RecyclervIEw实现此目的

当您想要更改RecyclervIEw的布局时,只需更改RecyclervIEw的LayoutManager和vIEwType即可

示例代码

试试这种方式

首先为多个vIEwType创建三个布局

grID_layout

<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.CardVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.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>

cardList_layout

<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.CardVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.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>

Title_layout

<?xml version="1.0" enCoding="utf-8"?><androID.support.v7.Widget.CardVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.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 , TitleLayout , 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>

DataAdapter

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, parent, 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, parent, 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, parent, 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);        }    }}

MainActivity

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", "Cards List", "GrID VIEw"};            builder.setItems(layouts, new DialogInterface.OnClickListener() {                @OverrIDe                public voID onClick(DialogInterface dialog, 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);    }}

activity_main layout

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tools="http://schemas.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

https://youtu.be/L3slKTy2bzI

总结

以上是内存溢出为你收集整理的android – 单击AlertDialog项目列表时在RecyclerView布局之间切换全部内容,希望文章能够帮你解决android – 单击AlertDialog项目列表时在RecyclerView布局之间切换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1112540.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存