仅我的viewpager

时间:2016-09-05 13:15:47

标签: android android-fragments android-viewpager android-progressbar

我设置了一个包含3个片段的viewpager。我的viewpager的第一页包含数据库中的数据。如何在加载数据之前仅为第一页实现进度条。

自定义片段=>

public class HomeContentFragment extends Fragment {
    public static ArrayList<ShishaBar> arrayList;
    public static ContentAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
                R.layout.recycler_view, container, false);
        adapter = new ContentAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        new BackgroundWorker().execute();
        return recyclerView;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView picture;
        public TextView name;
        public TextView description;
        public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.item_card, parent, false));
            picture = (ImageView) itemView.findViewById(R.id.card_image);
            name = (TextView) itemView.findViewById(R.id.card_title);
            description = (TextView) itemView.findViewById(R.id.card_text);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context ctx = v.getContext();
                    Intent intent = new Intent(ctx, DetailActivity.class);
                    intent.putExtra(DetailActivity.EXTRA_POSITION, getAdapterPosition());
                    ctx.startActivity(intent);
                }
            });
        }
    }

    public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {

        public ContentAdapter() {
            arrayList = new ArrayList<>();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.picture.setImageBitmap(arrayList.get(position).getImage());
            holder.name.setText(arrayList.get(position).getName());
            holder.description.setText(arrayList.get(position).getStraat()+ " " + arrayList.get(position).getHuisnr()+ ", "+arrayList.get(position).getStad());
        }

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


}

如果我在viewpager上放置了一个进度条,所有页面都会获得进度条,我只想在第一页上找到它。如果我将进度条放在我的card_view的xml中,则在数据加载之前您无法看到进度条。

1 个答案:

答案 0 :(得分:1)

ViewPager的适配器中,检查页面的索引,如:

public void instantiateItem(...,int position){
    if(position == 0){
         /* inflate a view that contains the ProgressBar and process it */
    ...
    }
}