当前位置: 首页 > 知识库问答 >
问题:

在回收器视图中查看寻呼机未在android中显示数据

章昊
2023-03-14

我需要制作一个电子商务应用程序类型的屏幕,具有多个viewpager和recycler视图。

我实现了它,但我的1/2视图寻呼机在回收器视图中可见,一旦我旋转设备,我的第一个视图寻呼机也会消失。

我的活动:

public class MainActivity extends AppCompatActivity {

private List<ProductEntity> productEntities = new ArrayList<>();
private RecyclerView productlist;
private HomeAdapter homeAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    productlist = (RecyclerView)findViewById(R.id.mainlist);
    //productlist.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    productlist.setLayoutManager(mLayoutManager);
    homeAdapter = new HomeAdapter(this,getSupportFragmentManager(),productEntities);
    productlist.setAdapter(homeAdapter);
    new ParserJsonTask().execute();
}

public  String loadJSONFromAsset() {
    StringBuffer sb = new StringBuffer();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open(
                "f_two.json")));
        String temp;
        while ((temp = br.readLine()) != null)
            sb.append(temp);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close(); // stop reading
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

private class ParserJsonTask extends AsyncTask<Void, Integer, List<ProductEntity>> {
    @Override
    protected List<ProductEntity> doInBackground(Void... params) {

        try{
            return JsonParser.getInstance().parseJson(loadJSONFromAsset());

        }catch(Exception e){
            Log.d("Exception", e.toString());
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<ProductEntity> result) {
        Log.e("Length",""+result.size());
        if(result != null) {
            productEntities = result;
            homeAdapter.setProduct(productEntities);
        }
    }
}

我的视图寻呼机片段:

public class ImageFragment extends Fragment {
/**
 * The fragment argument representing the section number for this
 * fragment.
 */
private static final String ARG_IMAGE_URL = "image_url";
private Context context;
private String image_url;
public ImageFragment() {
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context = context;
}

/**
 * Returns a new instance of this fragment for the given section
 * number.
 */
public static ImageFragment newInstance(String image) {
    ImageFragment fragment = new ImageFragment();
    Bundle args = new Bundle();
    args.putString(ARG_IMAGE_URL, image);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        image_url = getArguments().getString(ARG_IMAGE_URL);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_image, container, false);
    ImageView image = (ImageView) rootView.findViewById(R.id.image);
    Log.e("Pager Item Image",""+image_url);
    if(!TextUtils.isEmpty(image_url))
    Picasso.with(context).load(image_url).into(image);
    return rootView;
}
}

我的回收器视图的主适配器:

public class HomeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ProductEntity> productEntities;
private Context context;
private FragmentManager fragmentManager;
private OnItemClickListener mItemClickListener;
private static final int TYPE_FIRST = 0;
private static final int TYPE_SECOND = 1;
private static final int TYPE_THIRD = 2;
private static final String TEMPLATE_FIRST ="product-template-1";
private static final String TEMPLATE_SECOND ="product-template-2";
private static final String TEMPLATE_THIRD ="product-template-3";
public HomeAdapter(Context context , FragmentManager fragmentManager ,List<ProductEntity> productEntities) {
    this.productEntities = productEntities;
    this.context = context;
    this.fragmentManager = fragmentManager;
}

@Override
public int getItemCount() {
    Log.e("Item count",""+productEntities.size());
    return productEntities.size();
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if(holder instanceof ImageViewHolder) {
        ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
        ProductEntity productEntity = productEntities.get(position);
        Log.e("Template Item Image",""+productEntity.getItemEntities().get(0).getImage());
        Picasso.with(context).load(productEntity.getItemEntities().get(0).getImage()).into(imageViewHolder.imageView);
    } else if(holder instanceof PagerViewHolder) {
        PagerViewHolder pagerViewHolder = (PagerViewHolder) holder;
        ProductEntity productEntity = productEntities.get(position);
        pagerViewHolder.viewPager.setAdapter(new PagerAdapter(fragmentManager,productEntity.getItemEntities()));
    } else if (holder instanceof HorizontalViewHolder) {
        HorizontalViewHolder horizontalViewHolder = (HorizontalViewHolder) holder;
        ProductEntity productEntity = productEntities.get(position);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        horizontalViewHolder.recyclerView.setLayoutManager(linearLayoutManager);
        horizontalViewHolder.recyclerView.setAdapter(new SliderAdapter(context,productEntity.getItemEntities()));
    }

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
     if(viewType == TYPE_FIRST) {
        View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.content_image, parent, false);
        return new ImageViewHolder (v);
    } else if(viewType == TYPE_SECOND) {
        View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.content_horizontal, parent, false);
        return new HorizontalViewHolder (v);
    } else {
         View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.content_pager, parent, false);
         return new PagerViewHolder(v);
     }
}

@Override
public int getItemViewType (int position) {
    if(productEntities.get(position).getTemplate().equalsIgnoreCase(TEMPLATE_FIRST)) {
        Log.e("Item type",""+TYPE_FIRST);
        return TYPE_FIRST;
    } else if(productEntities.get(position).getTemplate().equalsIgnoreCase(TEMPLATE_SECOND)) {
        Log.e("Item type",""+TYPE_SECOND);
        return TYPE_SECOND;
    } else {
        Log.e("Item type",""+TYPE_THIRD);
        return TYPE_THIRD;
    }
}

class PagerViewHolder extends RecyclerView.ViewHolder {
    protected ViewPager viewPager;

    public PagerViewHolder (View itemView) {
        super (itemView);
        this.viewPager = (ViewPager) itemView.findViewById (R.id.viewpager);
    }
}

class ImageViewHolder extends RecyclerView.ViewHolder {
    protected ImageView imageView;

    public ImageViewHolder (View itemView) {
        super (itemView);
        this.imageView = (ImageView) itemView.findViewById (R.id.image);
    }
}

public  class HorizontalViewHolder extends RecyclerView.ViewHolder {

    protected RecyclerView recyclerView;

    public HorizontalViewHolder(View v) {
        super(v);
        recyclerView = (RecyclerView)v.findViewById(R.id.horizontallist);
    }
}

public void setProduct(List<ProductEntity> data) {
    productEntities.clear();
    productEntities.addAll(data);
    notifyDataSetChanged();
}

public interface OnItemClickListener {
    void onItemClick(View view, int position);
}

public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
}
}

我的视图寻呼机适配器:

public class PagerAdapter extends FragmentPagerAdapter {

private List<ItemEntity> itemEntities;
public PagerAdapter(FragmentManager fm, List<ItemEntity> itemEntities) {
    super(fm);
    this.itemEntities = itemEntities;
}

@Override
public Fragment getItem(int position) {
    // getItem is called to instantiate the fragment for the given page.
    // Return a PlaceholderFragment (defined as a static inner class below).
    return ImageFragment.newInstance(itemEntities.get(position).getImage());
}

@Override
public int getCount() {
    // Show 3 total pages.
    return itemEntities.size();
}
}

我的水平回收器视图适配器:

public class SliderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ItemEntity> itemEntities;
private Context context;
private OnItemClickListener mItemClickListener;
public SliderAdapter(Context context , List<ItemEntity> notesEntities) {
    this.itemEntities = notesEntities;
    this.context = context;
}

@Override
public int getItemCount() {
    return itemEntities.size();
}

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

        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        ItemEntity itemEntity = itemEntities.get(position);
        itemViewHolder.productName.setText(itemEntity.getLabel());
        Log.e("Item Image",""+itemEntity.getImage());
        if(!TextUtils.isEmpty(itemEntity.getImage()))
        Picasso.with(context).load(itemEntity.getImage()).into(itemViewHolder.productImage);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
        View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.recycler_card_layout, parent, false);
        return new ItemViewHolder (v);

}

public  class ItemViewHolder extends RecyclerView.ViewHolder {

    protected TextView productName;
    protected ImageView productImage;

    public ItemViewHolder(View v) {
        super(v);
        productName =  (TextView) v.findViewById(R.id.product_name);
        productImage = (ImageView)  v.findViewById(R.id.product_image);

        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null)
                    mItemClickListener.onItemClick(v, getAdapterPosition());
            }
        });

    }
}

public void setItems(List<ItemEntity> data) {
    itemEntities.clear();
    itemEntities.addAll(data);
    notifyDataSetChanged();

}

public interface OnItemClickListener {
    void onItemClick(View view, int position);
}

public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mItemClickListener = mItemClickListener;
}

}

主要活动布局:

<RelativeLayout 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="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.MainActivity"
tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
android:id="@+id/mainlist"
android:layout_width="match_parent"
android:layout_height="match_parent">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>

水平滚动布局:

<RelativeLayout 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="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activity.MainActivity"
tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
    android:id="@+id/horizontallist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>

查看寻呼机布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="200dp">
</android.support.v4.view.ViewPager>
</RelativeLayout>

根据我的JSON数据,在recycler视图中应该有2个视图寻呼机,但在纵向模式下只显示1个,一旦我旋转设备,这两个视图寻呼机都会消失。I thing数据在recycler视图中不可见,因为滚动时可以在recycler视图中看到空白。

任何帮助都将不胜感激。

共有1个答案

华升
2023-03-14

简单更改:

extends FragmentPagerAdapter

extends FragmentStatePagerAdapter
 类似资料:
  • 我想在垂直视图页中实现一个recyclerview。我当前的布局如下所示 如果我从Fragment1滑动到Fragment2,一切正常。我可以在回收器视图中上下滚动。如果我尝试滑动/滚动回Fragment1,就会出现问题。 如果用户已滚动到回收器视图的顶部,我想将下一个“向上”滚动事件转发到垂直视图页页面。我尝试过重写 方法,但遗憾的是仍然没有成功。有什么想法吗?感谢您的帮助:)

  • 我正试图添加数据在我的回收视图从火炉。我有带名字的文件(级别级别编号)但没有显示我的活动这是代码: 主要活动水平: 公共类级别活动扩展AppCompat活动{ } 水平调整器 公共类级别适配器扩展了RecyclerView。适配器 }

  • 我正在制作一个应用程序,其中有回收器视图。但我无法在我的应用程序中看到回收器视图。当我检查Logcat时,它显示'e/recyclerview:No adapter attached;正在跳过布局‘。为什么会发生这种情况,我该如何修复? 我的主要活动代码: 我的适配器(PostAdapter.java)类代码:

  • 在我的应用程序中,我使用了一个回收器视图,它包含了许多项目,我想在项目之间显示一个点分隔线(分隔符),但它不起作用。我尝试创建一个可绘制的形状,但在添加DividerItemDecurity之后,在回收器视图项之间没有显示空间或线条。我也尝试过创建自定义DividerItemDecoration类,但对我来说都不起作用。注意:目前在我的可绘制形状被设置为矩形,我也尝试了线条。如何实现。任何帮助都将

  • 我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我

  • 我怎样才能在Android中获得类似这样的东西? 我已经用深度页面转换器实现了一个viewpager,但是我想不出在后面添加项目预览的方法。