在recyclerView

时间:2016-01-31 01:52:48

标签: android android-studio

我要做的是拥有一个带有按钮的回收者视图。当初始化具有回收器的布局时,它将仅具有按钮,并且一旦被点击,将添加项目(第一项)和按钮(或图像视图,无论其上具有+)被推到右侧。我有recyclerView它正在工作(通过工作我的意思是我有一些照片以正确的顺序显示),但我无法显示按钮。我发现各种链接类似于此,例如How to add fixed Button in RecyclerView Adapter?,但我无法弄清楚。

这是适配器:

public class SelectPhotoAdapter  extends
        RecyclerView.Adapter<SelectPhotoHolder> {// Recyclerview will extend to
    // recyclerview adapter
    private ArrayList<Data_Model> arrayList;
    private Context context;

    private boolean hasLoadButton = true;
    private final int IMAGES = 0;
    private final int LOAD_MORE = 1;

    public SelectPhotoAdapter (Context context,
                                ArrayList<Data_Model> arrayList) {
        this.context = context;
        this.arrayList = arrayList;

    }


    public boolean isHasLoadButton() {
        return hasLoadButton;
    }

    public void setHasLoadButton(boolean hasLoadButton) {
        this.hasLoadButton = hasLoadButton;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (hasLoadButton) {
            return arrayList.size() + 1;
        } else {
            return arrayList.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < getItemCount()) {
            return IMAGES;
        } else {
            return LOAD_MORE;
        }
    }

//    @Override
//    public int getItemCount() {
//        return (null != arrayList ? arrayList.size() : 0);
//
//    }

    @Override
    public void onBindViewHolder(SelectPhotoHolder holder, int position) {
        SelectPhotoHolder mainHolder = holder;// holder
        if(position >= getItemCount()) {

            mainHolder.addPhoto.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    //dostuff
                }
            });
        } else {
            final Data_Model model = arrayList.get(position);

            Bitmap image = BitmapFactory.decodeResource(context.getResources(),
                    model.getImagePath());// This will convert drawbale image into
            mainHolder.imageview.setImageBitmap(image);
        }

    }




    @Override
public SelectPhotoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if(viewType == IMAGES) {

        return new SelectPhotoHolder((LayoutInflater.from(parent.getContext()).inflate(R.layout.selectphotolist, parent, false)),IMAGES);

    } else if (viewType == LOAD_MORE) {

        return new SelectPhotoHolder((LayoutInflater.from(parent.getContext()).inflate(R.layout.addphoto, parent, false)),LOAD_MORE);

    } else {
        return null;
    }
}


}

如果我使用评论的getItemCount()我会收到照片,但没有按钮,如果我使用从提供的链接中取消的未注释的getItemCount(),它就会崩溃。我还没弄清楚在哪里使用isHasLoadButton()setHasLoadButton方法。有人能指出我正确的方向吗?

如果您需要我发布任何文件,请告诉我。谢谢。

使用recyclerView的文件:

public class SelectPhotoDialogFragment extends DialogFragment {

    private RecyclerView mRecyclerView;
    private SelectPhotoAdapter adapter;
    // this method create view for your Dialog
    public static final Integer[] IMAGES= {R.drawable.first,R.drawable.second,R.drawable.third,R.drawable.picc};


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);

        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //inflate layout with recycler view
        View v = inflater.inflate(R.layout.select_photo, container, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
        int recyclerHeight = mRecyclerView.getHeight();

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        ArrayList<Data_Model> arrayList = new ArrayList<>();
        for (int i = 0; i < IMAGES.length; i++) {
            arrayList.add(new Data_Model(IMAGES[i]));
        }
        adapter = new SelectPhotoAdapter(getActivity(), arrayList);
        mRecyclerView.setAdapter(adapter);// set adapter on recyclerview
        adapter.notifyDataSetChanged();// Notify the adapter
        return v;
    }
}

再次解释:我唯一看到的是图像(第一,第二,第三,picc)和之后没有按钮,但它们显示正确。我想要发生的只是将按钮作为一个项目,当我点击它之前插入一个图片并向右移动(你可以看到我有一个水平方向)。这是我一直试图用2个ViewTypes做的,但我还没弄明白。

持有人:

    public class SelectPhotoHolder extends RecyclerView.ViewHolder  {
    // View holder for gridview recycler view as we used in listview
    private final int IMAGES = 0;
    private final int LOAD_MORE = 1;
    public ImageView imageview;

    public Button addPhoto;


    public SelectPhotoHolder(View view,int ViewType) {
        super(view);
        // Find all views ids
        if(ViewType == IMAGES) {
            this.imageview = (ImageView) view
                    .findViewById(R.id.selectPhoto);
        }
        else
            this.addPhoto = (Button) view.findViewById(R.id.load_more);

    }


}

新的持有人输出:

01-31 14:12:47.131 24072-24072/name.company.newapp E/test-exist: IMAGESfalse
01-31 14:12:47.141 24072-24072/name.company.newapp E/test-exist: IMAGESfalse
01-31 14:12:47.141 24072-24072/name.company.newapp E/test-exist: IMAGESfalse
01-31 14:12:47.151 24072-24072/name.company.newapp E/test-exist: IMAGESfalse
01-31 14:12:47.231 24072-24072/name.company.newapp E/test-exist: IMAGESfalse

2 个答案:

答案 0 :(得分:0)

@Override
public int getItemViewType(int position) {
    if (position < getItemCount()-1) {
        return IMAGES;
    } else {
        return LOAD_MORE;
    }
}
public SelectPhotoHolder(View view,int ViewType) {
    super(view);
    // Find all views ids
    if(ViewType == IMAGES) {
        this.imageview = (ImageView) view
                .findViewById(R.id.selectPhoto);
        Log.e("test-exist", "IMAGES" + (imageview  == null));
    }else{
        this.addPhoto = (Button) view.findViewById(R.id.load_more);
        Log.e("test-exist", "LOAD_MORE" + (addPhoto == null));
    }


}

 @Override
    public int getItemCount() {
        if (hasLoadButton) {
            return arrayList==null? 1 :arrayList.size() + 1;
        } else {
            return arrayList==null? 0 :arrayList.size();
        }
    }


     if(position >= (arrayList==null? 0 :arrayList.size())) {
        try{
            mainHolder.addPhoto.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    //dostuff
                }
            });
        }catch(Throwable e){
           Log.e("test-erorr","current position:"+position);
        }

    } else {
        final Data_Model model = arrayList.get(position);

        Bitmap image = BitmapFactory.decodeResource(context.getResources(),
                model.getImagePath());// This will convert drawbale image into
        mainHolder.imageview.setImageBitmap(image);
    }

答案 1 :(得分:0)

这是一个样本,这就是为什么我在&#34;示例 - 示例&#34;,根据您自己的要求进行修改,此&#34;示例&#34;显示带有图像的Recyclerview项目,在recyclerview下面有一个按钮,当按下它时会添加一个新图像,你可以修改地点 - 动画,这里是Item.java:

public class Item {

private int Pic;

public Item(String Title, int Pic, int Color) {
    setPic(Pic);
}


public int getPic() {
    return Pic;
}

public void setPic(int photo) {
    this.Pic= photo;
}

}

适配器:

public class Adapter extends RecyclerView.Adapter<RecyclerViewHolders>{

private List<Item> itemList;
public Context context;

public Adapter(Context context, List<Item> itemList) {
    this.itemList = itemList;
    this.context = context;
}

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

    View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.your_item, null);
    return new RecyclerViewHolders(layoutView);
}

@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
    holder.Pic.setImageResource(itemList.get(position).getPic());
}

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

}

现在,在MainActivity或适配器初始化

的情况下,这一切都正常
Adapter rcAdapter;
public static List<Item> Items;
public static Button button;

在你的onCreate:

initListViews();

现在是initListViews方法:

    public void initListViews()
{
    List<Item> rowListItem = getAllItemList();
    LinearLayoutManager yourlayout= new LinearLayoutManager(this);

    RecyclerView RcView = (RecyclerView)findViewById(R.id.recycler_view);
    button = (Button)findViewById(R.id.button);

    RcView.setHasFixedSize(true);
    RcView.setLayoutManager(yourlayout);
    rcAdapter = new RecyclerViewAdapter(this, rowListItem);
    RcView.setAdapter(rcAdapter);

}

 private List<Item> getAllItemList(){
    Items.add(new Item(YourImageResources));
    Items.add(new Item(YourImageResources));
        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AllItems.add(new Item(YourImageResources));

        }
    });

 }

如果您想要添加特定数量的项目:

int PressedTimes = 0;

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(PressedTimes < 7)
            {
                AllItems.add(new Item(YourImageResources));
                PressedTimes=+1;
            }
            if(PressedTimes == 7)
            {
                button.setVisibility(View.GONE);
            }

        }
    });