ModelSectionHotTrendingNews

ModelSectionHotTrendingNews

本文介绍了如何制作“查看所有项目"按钮将数据从RecyclerView传递到另一个RecyclerView片段-AndroidX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

Q 建议

AdapterSectionHotTrendingNews.java

public class AdapterSectionHotTrendingNews extends RecyclerView.Adapter{

private Context mContext;
private ArrayList<ModelSectionHotTrendingNews> modelSectionHotTrendingNews;

public AdapterSectionHotTrendingNews(Context mContext, ArrayList<ModelSectionHotTrendingNews> modelSectionHotTrendingNews) {
    this.mContext = mContext;
    this.modelSectionHotTrendingNews = modelSectionHotTrendingNews;
}

@Override
public SectionHotTrendingNewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_section_hottrendingnews, null);
    return new SectionHotTrendingNewsViewHolder(v);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

    SectionHotTrendingNewsViewHolder sectionHotTrendingNewsViewHolder = (SectionHotTrendingNewsViewHolder) holder;
    ModelSectionHotTrendingNews modelSectionHotTrendingNewsX = modelSectionHotTrendingNews.get(position);

    //Set
    sectionHotTrendingNewsViewHolder.TVSectionTitle.setText(modelSectionHotTrendingNewsX.getHeaderTitle());

    ArrayList singleSectionItems = modelSectionHotTrendingNewsX.getAllItemsInSection();
    /*AdapterSingleHotLatestNews.RecyclerViewClickListener listenerLatestNews;
    listenerLatestNews = new AdapterSingleHotLatestNews.RecyclerViewClickListener() {
        @Override
        public void onRowHotLatestNewsClick(View view, int position) {

        }

        @Override
        public void onIconHotLatestNewsClick(View view, int position) {

        }
    };*/
    AdapterSingleHotTrendingNews itemListDataAdapter = new AdapterSingleHotTrendingNews(mContext, singleSectionItems);

    /*itemRowHolder.recycler_view_list.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow ScrollView to intercept touch events.
                    v.getParent().requestDisallowInterceptTouchEvent(true);
                    break;
                case MotionEvent.ACTION_UP:
                    //Allow ScrollView to intercept touch events once again.
                    v.getParent().requestDisallowInterceptTouchEvent(false);
                    break;
            }
            // Handle RecyclerView touch events.
            v.onTouchEvent(event);
            return true;
        }
    });*/


    final String sectionTitle = modelSectionHotTrendingNewsX.getHeaderTitle();
    sectionHotTrendingNewsViewHolder.BTNHotTrendingNewsMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "click event on more, "+sectionTitle, Toast.LENGTH_SHORT).show();
        }
    });


    /*Glide.with(mContext)
            .load(feedItem.getImageURL())
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .error(R.drawable.bg)
            .into(feedListRowHolder.thumbView);*/

    //SetRecyclerView
    sectionHotTrendingNewsViewHolder.RecyclerViewSectionHotTrendingNews.setHasFixedSize(true);
    sectionHotTrendingNewsViewHolder.RecyclerViewSectionHotTrendingNews.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
    sectionHotTrendingNewsViewHolder.RecyclerViewSectionHotTrendingNews.setAdapter(itemListDataAdapter);
    sectionHotTrendingNewsViewHolder.RecyclerViewSectionHotTrendingNews.setNestedScrollingEnabled(false);
}

@Override
public int getItemCount() {
    int itemCount = modelSectionHotTrendingNews.size();
    return itemCount;
}

public class SectionHotTrendingNewsViewHolder extends RecyclerView.ViewHolder {

    protected TextView TVSectionTitle;
    protected ImageView BTNHotTrendingNewsMore;
    protected RecyclerView RecyclerViewSectionHotTrendingNews;

    public SectionHotTrendingNewsViewHolder(View itemView) {
        super(itemView);

        TVSectionTitle = (TextView) itemView.findViewById(R.id.TV_SectionTitle);
        BTNHotTrendingNewsMore= (ImageView) itemView.findViewById(R.id.BTN_HotTrendingNewsMore);
        RecyclerViewSectionHotTrendingNews = (RecyclerView) itemView.findViewById(R.id.RecyclerView_Section_HotTrendingNews);
    }

}

}

AdapterSingleHotTrendingNews.java

public class AdapterSingleHotTrendingNews extends RecyclerView.Adapter{

private Context mContext;
private ArrayList<ModelSingleHotTrendingNews> modelSingleHotTrendingNews;

public AdapterSingleHotTrendingNews(Context mContext, ArrayList<ModelSingleHotTrendingNews> modelSingleHotTrendingNews) {
    this.mContext = mContext;
    this.modelSingleHotTrendingNews = modelSingleHotTrendingNews;
}

//Container
@Override
public SingleHotTrendingNewsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_single_hottrendingnews, null);
    return new SingleHotTrendingNewsViewHolder(v);
}

//Fill Container with Model Setter Getter
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

    final SingleHotTrendingNewsViewHolder singleHotTrendingNewsViewHolder = (SingleHotTrendingNewsViewHolder) holder;
    final ModelSingleHotTrendingNews modelSingleHotTrendingNewsX = modelSingleHotTrendingNews.get(position);

    //Set
    singleHotTrendingNewsViewHolder.TVGameDate.setText(modelSingleHotTrendingNewsX.getGamedate());
    singleHotTrendingNewsViewHolder.TVGameDescription.setText(modelSingleHotTrendingNewsX.getGamedescription());
    singleHotTrendingNewsViewHolder.IMGGameImage.setImageResource(modelSingleHotTrendingNewsX.getGameimage());



    singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final Boolean booltrendingnewssaving = modelSingleHotTrendingNewsX.getTrendingnewssaving();

            final int id = modelSingleHotTrendingNewsX.getId();

            if (booltrendingnewssaving == true){
                Toast.makeText(v.getContext(), "Turn Off Saved News " + id, Toast.LENGTH_SHORT).show();
                singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setImageResource(R.drawable.saved_off);
                modelSingleHotTrendingNewsX.setTrendingnewssaving(false);
                /*updateSavedNewsSaving("update_newssaving", id, false);*/
            } else if(booltrendingnewssaving == false) {
                Toast.makeText(v.getContext(), "Turn On Saved News " + id, Toast.LENGTH_SHORT).show();
                singleHotTrendingNewsViewHolder.ICONHotTrendingNewsSaving.setImageResource(R.drawable.saved_on);
                modelSingleHotTrendingNewsX.setTrendingnewssaving(true);
                /*updateSavedNewsSaving("update_newssaving", id, true);*/
            }
        }
    });



   /* Glide.with(mContext)
            .load(feedItem.getImageURL())
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .centerCrop()
            .error(R.drawable.bg)
            .into(feedListRowHolder.thumbView);*/

    singleHotTrendingNewsViewHolder.ROWHotTrendingNewsContainer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), modelSingleHotTrendingNews.get(position).getGamedescription(), Toast.LENGTH_SHORT).show();

            //Passing Data to GameDescriptionActivity
            Intent intent = new Intent(mContext, GameDetailActivity.class);
            intent.putExtra("ImagePKG",  modelSingleHotTrendingNews.get(position).getGameimage());
            intent.putExtra("NamePKG",  modelSingleHotTrendingNews.get(position).getGamedate());
            intent.putExtra("UrlPKG",  modelSingleHotTrendingNews.get(position).getId());
            intent.putExtra("DescriptionPKG",  modelSingleHotTrendingNews.get(position).getGamedescription());
            mContext.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    int itemCount = modelSingleHotTrendingNews.size();
    return itemCount;
}

public class SingleHotTrendingNewsViewHolder extends RecyclerView.ViewHolder{
    TextView TVGameDate;
    TextView TVGameDescription;
    ImageView IMGGameImage;

    ImageView ICONHotTrendingNewsSaving;
    private RelativeLayout ROWHotTrendingNewsContainer;


    public SingleHotTrendingNewsViewHolder(View itemView) {
        super(itemView);

        TVGameDate = itemView.findViewById(R.id.TV_GameDate);
        TVGameDescription = itemView.findViewById(R.id.TV_GameDescription);
        IMGGameImage = itemView.findViewById(R.id.IMG_GameImage);

        ICONHotTrendingNewsSaving = itemView.findViewById(R.id.ICON_HotTrendingNewsSaving);
        ROWHotTrendingNewsContainer = itemView.findViewById(R.id.ROW_HotTrendingNewsContainer);
    }

}

ModelSectionHotTrendingNews.java

public class ModelSectionHotTrendingNews {

private String headerTitle;
private ArrayList<ModelSingleHotTrendingNews> allItemsInSection;

public ModelSectionHotTrendingNews() {
}

public String getHeaderTitle() {
    return headerTitle;
}

public void setHeaderTitle(String headerTitle) {
    this.headerTitle = headerTitle;
}

public ArrayList<ModelSingleHotTrendingNews> getAllItemsInSection() {
    return allItemsInSection;
}

public void setAllItemsInSection(ArrayList<ModelSingleHotTrendingNews> allItemsInSection) {
    this.allItemsInSection = allItemsInSection;
}

}

ModelSingleHotTrendingNews.java

public class ModelSingleHotTrendingNews {

private int id;
private String gamedate;
private String gamedescription;
private int gameimage;

private Boolean trendingnewssaving;
private String value;
private String message;

public ModelSingleHotTrendingNews(int id, String gamedate, String gamedescription, int gameimage, Boolean trendingnewssaving, String value, String message) {
    this.id = id;
    this.gamedate = gamedate;
    this.gamedescription = gamedescription;
    this.gameimage = gameimage;
    this.trendingnewssaving = trendingnewssaving;
    this.value = value;
    this.message = message;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getGamedate() {
    return gamedate;
}

public void setGamedate(String gamedate) {
    this.gamedate = gamedate;
}

public String getGamedescription() {
    return gamedescription;
}

public void setGamedescription(String gamedescription) {
    this.gamedescription = gamedescription;
}

public int getGameimage() {
    return gameimage;
}

public void setGameimage(int gameimage) {
    this.gameimage = gameimage;
}

public Boolean getTrendingnewssaving() {
    return trendingnewssaving;
}

public void setTrendingnewssaving(Boolean trendingnewssaving) {
    this.trendingnewssaving = trendingnewssaving;
}

public String getValue() {
    return value;
}

public void setValue(String value) {
    this.value = value;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

}

FragmentFunction

public void createDummyData1() {
    ModelSectionHotTrendingNews modelSectionHotTrendingNewsX = new ModelSectionHotTrendingNews();
    modelSectionHotTrendingNewsX.setHeaderTitle("TRENDING NEWS");

    ArrayList<ModelSingleHotTrendingNews> modelSingleHotTrendingNews = new ArrayList<ModelSingleHotTrendingNews>();
    modelSingleHotTrendingNews.add(new ModelSingleHotTrendingNews(1, "20 Mei 2019", "sdfjiaOIJdosjfsjdfjsoid", R.drawable.kleptocats, false, "x53535", "No Error"));
    modelSingleHotTrendingNews.add(new ModelSingleHotTrendingNews(2, "21 Mei 2019", "sdfjiaOIJdosjfsjdfjsoid", R.drawable.kleptocats, false, "x53535", "No Error"));
    modelSingleHotTrendingNews.add(new ModelSingleHotTrendingNews(3, "22 Mei 2019", "sdfjiaOIJdosjfsjdfjsoid", R.drawable.kleptocats, false, "x53535", "No Error"));
    modelSingleHotTrendingNews.add(new ModelSingleHotTrendingNews(4, "23 Mei 2019", "sdfjiaOIJdosjfsjdfjsoid", R.drawable.kleptocats, false, "x53535", "No Error"));
    modelSingleHotTrendingNews.add(new ModelSingleHotTrendingNews(5, "24 Mei 2019", "sdfjiaOIJdosjfsjdfjsoid", R.drawable.kleptocats, false, "x53535", "No Error"));
    modelSingleHotTrendingNews.add(new ModelSingleHotTrendingNews(6, "25 Mei 2019", "sdfjiaOIJdosjfsjdfjsoid", R.drawable.kleptocats, false, "x53535", "No Error"));
    modelSingleHotTrendingNews.add(new ModelSingleHotTrendingNews(7, "26 Mei 2019", "sdfjiaOIJdosjfsjdfjsoid", R.drawable.kleptocats, false, "x53535", "No Error"));


    modelSectionHotTrendingNewsX.setAllItemsInSection(modelSingleHotTrendingNews);
    modelSectionHotTrendingNews.add(modelSectionHotTrendingNewsX);
}

//已更新我在SectionAdapterTabHot.java上添加了它我使用相同的模型,但使用了不同的RecyclerView和Adapter,例如:RecyclerViewMoreSection/SingleTabHot和Section/SingleMoreAdapterTabHot.java

//UPDATEDI Added this on SectionAdapterTabHot.javaI use same model but different RecyclerView and Adapter like: RecyclerViewMoreSection/SingleTabHot and Section/SingleMoreAdapterTabHot.java

final String sectionTitle = modelSectionHotTrendingNewsX.getHeaderTitle();
    sectionHotTrendingNewsViewHolder.BTNHotTrendingNewsMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(v.getContext(), "click event on more, "+sectionTitle, Toast.LENGTH_SHORT).show();
            sectionHotTrendingNewsViewHolder.RecyclerViewMoreSectionHotTrendingNews.setAdapter( new AdapterMoreSingleHotTrendingNews(v.getContext(), modelSectionHotTrendingNews.get(position).getAllItemsInSection()));
            Intent intent = new Intent(mContext, TabMoreHotActivity.class);
            mContext.startActivity(intent);
        }
    });

//SectionAdapterViewHolder

//SectionAdapterViewHolder

public class SectionHotTrendingNewsViewHolder extends RecyclerView.ViewHolder {

    protected TextView TVSectionTitle;
    protected ImageView BTNHotTrendingNewsMore;
    protected RecyclerView RecyclerViewSectionHotTrendingNews;
    protected RecyclerView RecyclerViewMoreSectionHotTrendingNews;

    public SectionHotTrendingNewsViewHolder(View itemView) {
        super(itemView);

        TVSectionTitle = (TextView) itemView.findViewById(R.id.TV_SectionTitle);
        BTNHotTrendingNewsMore= (ImageView) itemView.findViewById(R.id.BTN_HotTrendingNewsMore);
        RecyclerViewSectionHotTrendingNews = (RecyclerView) itemView.findViewById(R.id.RecyclerView_Section_HotTrendingNews);
        RecyclerViewMoreSectionHotTrendingNews = (RecyclerView) itemView.findViewById(R.id.RecyclerViewMore_Section_HotTrendingNews);
    }

}

///这是我要访问itemmore_hot中的RecyclerViewMoreHotTrendingNews的问题,但它不在item_hot中建立

//This is the problem I want to Access RecyclerViewMoreHotTrendingNews in itemmore_hot but it doesn't founded in item_hot

@Override
public SectionHotTrendingNewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_section_hottrendingnews, null);
    return new SectionHotTrendingNewsViewHolder(v);
}

//这是TabMoreHotActivity.java

//This is TabMoreHotActivity.java

public class TabMoreHotActivity extends AppCompatActivity {

//1
ArrayList<ModelSectionHotTrendingNews> modelSectionHotTrendingNews = new ArrayList<ModelSectionHotTrendingNews>();
AdapterMoreSectionHotTrendingNews adapterMoreSectionHotTrendingNews;

//ModelData
List<ModelSingleHotTrendingNews> modelSingleHotTrendingNews;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragmentmore_tabhot);


    //1
    RecyclerView RecyclerViewMoreSingleHotTrendingNews = findViewById(R.id.RecyclerViewMore_Single_HotTrendingNews);
    RecyclerViewMoreSingleHotTrendingNews.setHasFixedSize(true);
    adapterMoreSectionHotTrendingNews = new AdapterMoreSectionHotTrendingNews(this, modelSectionHotTrendingNews);
    RecyclerViewMoreSingleHotTrendingNews.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
    RecyclerViewMoreSingleHotTrendingNews.setAdapter(adapterMoreSectionHotTrendingNews);
    //Optimized
    RecyclerViewMoreSingleHotTrendingNews.setHasFixedSize(true);
    RecyclerViewMoreSingleHotTrendingNews.setItemViewCacheSize(20);
}

}

//但是当我单击查看所有项目"按钮时出现此错误

//But I got This error when I click "See All Item" button

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.indofun.android.indojoy, PID: 11456
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
    at com.indofun.android.indojoy.Adapters.Adapter_HotTrendingNews.AdapterSectionHotTrendingNews$1.onClick(AdapterSectionHotTrendingNews.java:88)
    at android.view.View.performClick(View.java:6891)
    at android.view.View$PerformClick.run(View.java:26083)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

推荐答案

从第一个适配器onClick中,调用第二个适配器并更新RecyclerView

From First Adapter onClick, call second adapter and update RecyclerView

sectionHotTrendingNewsViewHolder.BTNHotTrendingNewsMore.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sectionHotTrendingNewsViewHolder.RecyclerViewSectionHotTrendingNews.setAdapter( new AdapterSingleHotTrendingNews(v.getContext(), modelSectionHotTrendingNews.get(position).getAllItemsInSection()))
        }
    });

或者如果您想在其他活动中显示此更多新闻.然后执行以下操作:

Or if you want to show this More News in another activity. Then do this:

  1. 使 ModelSingleHotTrendingNews Serializable像这样:

公共类ModelSingleHotTrendingNews实现了Serializable { ...}

public class ModelSingleHotTrendingNews implements Serializable { ...}

更改onClick

sectionHotTrendingNewsViewHolder.BTNHotTrendingNewsMore.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Intent intent = new Intent(mContext,TabMoreHotActivity.class); Intent().putExtra("MoreNews",modelSectionHotTrendingNews.get(position).getAllItemsInSection()) mContext.startActivity(intent); } });

sectionHotTrendingNewsViewHolder.BTNHotTrendingNewsMore.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(mContext, TabMoreHotActivity.class); Intent().putExtra("MoreNews", modelSectionHotTrendingNews.get(position).getAllItemsInSection()) mContext.startActivity(intent); } });

TabMoreHotActivity中使用ArrayList代替ArrayList<ModelSectionHotTrendingNews>

In TabMoreHotActivity use ArrayList instead of ArrayList<ModelSectionHotTrendingNews>

希望这会对您有所帮助.您弄乱了代码:(

hope this will help you. You made your code mess :(

这篇关于如何制作“查看所有项目"按钮将数据从RecyclerView传递到另一个RecyclerView片段-AndroidX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:11