android 单击回收时创建图像视图使用ViewPager2查看选项卡中的项目布局

mv1qrgav  于 2022-11-20  发布在  Android
关注(0)|答案(1)|浏览(143)

First, my app is like this.我想添加imageview到片段1当我点击recyclerview项目,但imageview不出现我不知道的问题提前谢谢!
我试试这个:

空间编辑适配器.java(回收视图适配器)

public class SpaceEditAdapter extends RecyclerView.Adapter<SpaceEditAdapter.ItemViewHolder> {
   public ArrayList<SpaceEditData> spaceData =new ArrayList<>();

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType ){
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_space_edit_item,parent,false);
    }
    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position){
        holder.onBind(spaceData.get(position));
    }
    @Override
    public int getItemCount(){
        return spaceData.size();
    }
    public void addItem(SpaceEditData d){
        spaceData.add(d);
    }

    class ItemViewHolder extends RecyclerView.ViewHolder{

       ImageView myItemImageView;
        ItemViewHolder(View itemView){
            super(itemView);
  
            myItemImageView=itemView.findViewById(R.id.myItemImg);
            myItemImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = getAdapterPosition() ;
                    if (pos != RecyclerView.NO_POSITION) {
                        SpaceEditData item=spaceData.get(pos);
                        Bundle bundle=new Bundle();
                        bundle.putSerializable("num", item);
                        SpaceFragment sf=new SpaceFragment();
                        sf.setArguments(bundle);

                    }
                }
            });

        }
        void onBind(SpaceEditData data){
            myItemImageView.setImageResource(data.getMyItemImg());

        }
    }
}

SpaceFragment.java(图像中的片段1)

public class SpaceFragment extends Fragment {
    private View v;
    private ViewPager2 viewPager;
    private TabLayout tabLayout;
    private SpaceEditPaintFragment paintF;
    private SpaceEditForestFragment forestF;
    private SpaceEditSeaFragment seaF;
    private SpaceEditMudFragment mudF;
    private int count=1;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        v=inflater.inflate(R.layout.fragment_space, container, false);
       
        createFragment();
        createViewPager();
        settingTabLayout();

        ImageView imageView = new ImageView(v.getContext());
        Bundle bundle=getArguments();

        if(bundle!=null) {
           SpaceEditData data=(SpaceEditData)bundle.getSerializable("num");
            ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(layoutParams);
            imageView.setImageResource(data.getMyItemImg());
            spaceLay.addView(imageView);
        }

        return v;
    }
    private void createFragment(){
        paintF=new SpaceEditPaintFragment();
        forestF=new SpaceEditForestFragment();
        seaF=new SpaceEditSeaFragment();
        mudF=new SpaceEditMudFragment();
    }
    private void createViewPager(){
        viewPager = (ViewPager2)v.findViewById(R.id.spaceEditViewpager);
        SpaceEditViewPagerAdapter pagerAdapter=new SpaceEditViewPagerAdapter(getFragmentManager(),getLifecycle());
        pagerAdapter.addFragment(paintF);
        pagerAdapter.addFragment(forestF);
        pagerAdapter.addFragment(seaF);
        pagerAdapter.addFragment(mudF);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setUserInputEnabled(false);
    }
    private void settingTabLayout(){
        tabLayout=(TabLayout)v.findViewById(R.id.spaceEditTabLayout);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int pos = tab.getPosition();
                switch(pos){
                    case 0:
                        viewPager.setCurrentItem(0);
                        break;
                    case 1:
                        viewPager.setCurrentItem(1);
                        break;
                    case 2:
                        viewPager.setCurrentItem(2);
                        break;
                    case 3:
                        viewPager.setCurrentItem(3);
                        break;
                }
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }

}

空间编辑数据.java(循环检视项目)

import java.io.Serializable;

public class SpaceEditData implements Serializable {

    private int myItemImg;

    public int getMyItemImg() {
        return myItemImg;
    }

    public void setMyItemImg(int myItemImg) {
        this.myItemImg = myItemImg;
    }
}

片段空间.xml(图像中的片段1)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/spaceShopFLay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/lightBeige"
    tools:context=".SpaceFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/spaceLay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/lightBeige">

        <ImageView
            android:id="@+id/spaceShop"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginStart="280dp"
            android:layout_marginLeft="280dp"
            android:layout_marginTop="20dp"
            android:clickable="true"
            android:contentDescription="@string/imageView"
            android:focusable="true"
            android:src="@drawable/ic_shop"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/spaceEdit"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginTop="20dp"
            android:layout_marginStart="25dp"
            android:layout_marginLeft="25dp"
            android:contentDescription="@string/imageView"
            android:src="@drawable/ic_edit"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@id/spaceShop"
            android:clickable="true"
            android:focusable="true" />

        <com.example.zeve.StickerView
            android:id="@+id/stickerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/spaceEditLay"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="404dp"
        android:background="@color/brown">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/spaceEditTabLayout"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tabGravity="center">

            <com.google.android.material.tabs.TabItem
                android:id="@+id/editPaintTab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/paint"></com.google.android.material.tabs.TabItem>

            <com.google.android.material.tabs.TabItem
                android:id="@+id/editForestTab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/forest"></com.google.android.material.tabs.TabItem>

            <com.google.android.material.tabs.TabItem
                android:id="@+id/editSeaTab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sea"></com.google.android.material.tabs.TabItem>

            <com.google.android.material.tabs.TabItem
                android:id="@+id/editMudTab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/mud"></com.google.android.material.tabs.TabItem>

        </com.google.android.material.tabs.TabLayout>

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/spaceEditViewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            android:layout_marginTop="40dp"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

</FrameLayout>

片段空间编辑项目.xml(回收查看项目xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="75dp"
    android:layout_height="60dp"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/myItemImg"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="5dp"
        android:layout_gravity="center"
        android:clickable="true"
        android:focusable="true"/>
    android:background="@color/lightBeige"
    android:contentDescription="@string/imageView"
</LinearLayout>
lo8azlld

lo8azlld1#

这很愚蠢,但是在您的fragment_space_edit_item.xml

<ImageView
    android:id="@+id/myItemImg"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="5dp"
    android:layout_gravity="center"
    android:clickable="true"
    android:focusable="true"/>
android:background="@color/lightBeige"
android:contentDescription="@string/imageView"

如果是这种情况,则background和contentDescription为Error。

相关问题