Android:如何在回收站布局适配器中动态填充片段?

时间:2016-10-15 11:50:46

标签: android android-layout android-fragments android-recyclerview

我正在尝试以列表视图的形式提供Recycler视图,我想设置一个片段以在列表下显示更多详细信息

这是我的适配器布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="2dp"
    android:id ="@+id/relative_layout_text"
    >
    <ImageView
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:id="@+id/image_view_food_image"
        android:layout_alignParentLeft="true"
        android:background="@drawable/common_google_signin_btn_icon_dark_normal"/>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linear_layout_text"
        android:layout_toRightOf="@+id/image_view_food_image">

        <TextView
            android:id="@+id/text_view_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="19dp"
            android:textStyle="bold"
            android:textColor="@color/colorTitle"
            android:layout_alignParentTop="true"/>
        <TextView
            android:id="@+id/text_view_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:layout_alignParentTop="true"
            android:layout_marginTop="5dp"/>
    </LinearLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fragments"
        android:layout_below="@+id/linear_layout_text"></FrameLayout>

</RelativeLayout>

以及我如何在适配器中启动我的片段

public class FirebaseAdapter extends RecyclerView.Adapter<FirebaseAdapter.MyViewHolder> {

    private int lastPosition = -1;
    private View views ;
    private List<Foods> FoodArray = new ArrayList<>();
    private Context mContext ;
    private ArrayList<String> itemsDesc = new ArrayList<>();

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title,name;
    public ImageView foodImage;
    public RelativeLayout relativeLayout;
    public FrameLayout frameLayout;

    public MyViewHolder(View view) {
        super(view);

        title = (TextView) view.findViewById(R.id.text_view_title);
        name = (TextView) view.findViewById(R.id.text_view_name);
        foodImage = (ImageView) view.findViewById(R.id.image_view_food_image);
        relativeLayout = (RelativeLayout) view.findViewById(R.id.relative_layout_text);
        frameLayout = (FrameLayout) view.findViewById(R.id.fragments);
        views = view;
    }
}

public FirebaseAdapter(Context context,List<Foods> Foods){
    this.FoodArray = Foods;
    this.mContext = context;
}


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.menu_list, parent, false);
    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    holder.title.setText(FoodArray.get(position).getTitle());
    holder.name.setText(FoodArray.get(position).getName());



    itemsDesc.add(FoodArray.get(position).getTitle());
    itemsDesc.add(FoodArray.get(position).getName());
    itemsDesc.add(FoodArray.get(position).getURL());

    DownloadImage Downloader = new DownloadImage(holder.foodImage);

    Downloader.execute(FoodArray.get(position).getURL());

    setAnimation(holder.relativeLayout, position);


    Fragment blankFragment = new BlankFragment();
    Bundle bundle = new Bundle();
    bundle.putStringArrayList("Food",itemsDesc);
    blankFragment.setArguments(bundle);
    ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction()
            .add(holder.frameLayout.getId(),blankFragment,"BlankFragment").addToBackStack(null).commit();
}

//this is to download async
public class DownloadImage extends AsyncTask<String,Void,Bitmap>{
        private ImageView mImageView;

        public DownloadImage(ImageView imageView){
            this.mImageView = imageView;
        }

        //this is the code for downloading the image
        @Override
        protected Bitmap doInBackground(String ... url){
            Bitmap Image = null;
            try{
                InputStream is = new URL(url[0]).openStream();
                Image = BitmapFactory.decodeStream(is);

            }catch(MalformedURLException ex){
                ex.printStackTrace();
            }
            catch(IOException ex ){
                ex.printStackTrace();
            }
            return Image;
        }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (result != null) {
            mImageView.setImageBitmap(result);
        }
    }
}

private void setAnimation(View viewToAnimate, int position)
{
    // If the bound view wasn't previously displayed on screen, it's animated
    if (position > lastPosition)
    {
        Animation animation = AnimationUtils.loadAnimation(viewToAnimate.getContext(), android.R.anim.slide_in_left);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }
}
@Override
public int getItemCount(){return FoodArray.size();}   

}

并且是我片段的代码

public class BlankFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_blank,container,false);
        TextView title = (TextView)view.findViewById(R.id.text_view_title);
        TextView description = (TextView)view.findViewById(R.id.text_view_description);
        ImageView foodImage = (ImageView) view.findViewById(R.id.image_view_big_food_image);


        ArrayList<String> foods = getArguments().getStringArrayList("Food");

        title.setText(foods.get(0));
        description.setText(foods.get(1));

        DownloadImage Downloader = new DownloadImage(foodImage);

        Downloader.execute(foods.get(2));

        return view;
    }

    public class DownloadImage extends AsyncTask<String,Void,Bitmap> {
        private ImageView mImageView;

        public DownloadImage(ImageView imageView){
            this.mImageView = imageView;
        }

        //this is the code for downloading the image
        @Override
        protected Bitmap doInBackground(String ... url){
            Bitmap Image = null;
            try{
                InputStream is = new URL(url[0]).openStream();
                Image = BitmapFactory.decodeStream(is);

            }catch(MalformedURLException ex){
                ex.printStackTrace();
            }
            catch(IOException ex ){
                ex.printStackTrace();
            }
            return Image;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            if (result != null) {
                mImageView.setImageBitmap(result);
            }
        }
    }

因此,使用我当前的代码,我希望数据能够像这样

Expected results

但我得到了这个

this is what i got

所以我不确定如何制作它以便我能得到我预期的结果

请,谢谢并提前

1 个答案:

答案 0 :(得分:0)

伙计们我发现了我的问题并且没有解决方案,因为在stackoverflow here中这个人的问题中说明了他说在onBindView中初始化一个片段是可能的,但如果你这样做你就不能这样做我将只使用一个特定的ID,因为片段将堆叠在其上,而不是在每个回收商项目上添加一个