在Recycler视图中查看寻呼机不显示android中的数据

时间:2016-05-29 09:03:32

标签: android android-viewpager android-recyclerview

我需要创建一个包含多个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数据,它应该在回收器视图中有2个视图寻呼机,但它在纵向模式下只显示1个,一旦我旋转设备,两个都消失了。我在回收器视图中看不到东西数据,因为我可以在滚动时看到回收器视图中的空白区域。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:-2)

简单的改变:

extends FragmentPagerAdapter

extends FragmentStatePagerAdapter