在“回收者”视图中图像无法正确显示

时间:2018-09-25 10:58:33

标签: android android-recyclerview android-image

我的Recycler View遇到问题,图像无法正确显示,

这里是您可以查看我正在做什么的链接

  1. https://ibb.co/jXgXU9
  2. https://ibb.co/ceTCU9
  3. https://ibb.co/jkFsU9
  4. https://ibb.co/jkFsU9
  5. https://ibb.co/ffte99

在将“回收站”视图宽度更改为与父项匹配,将高度更改为wrap_content时,它起作用,但在向下滚动然后又向上滚动时,它会拧紧,并且再次仅显示一幅图像时间

  1. 这是customlayout.xml的代码

      <LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
    
    
       <ImageView
           android:id="@+id/listicon"
           android:layout_width="match_parent"
           android:layout_height="220dp" />
    
       </LinearLayout>
    
    1. 这是我的fragment_home.xml

      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".HomeFragment">
      <android.support.v7.widget.RecyclerView
         android:id="@+id/drawerList"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true">
      
         </android.support.v7.widget.RecyclerView>
      
          </FrameLayout>
      

3。这是HomeFragment.java

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    public class HomeFragment extends Fragment  {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private CustomAdapter adapter;
    int[] IMAGES = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
    private RecyclerView recyclerView;


    ArrayList<HashMap<String, String>> data;


    private String mParam1;
    private String mParam2;


    public HomeFragment() {

      }
    public static HomeFragment newInstance(String param1, String param2) {                   
    HomeFragment fragment = new HomeFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
        }
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
        }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View layout = inflater.inflate(R.layout.fragment_home, container, false);
     recyclerView =(RecyclerView) layout.findViewById(R.id.drawerList);
     adapter = new CustomAdapter(getActivity(), getData());
     recyclerView.setAdapter(adapter);
     recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    return layout;
      }

    public static List<Information> getData(){
    List<Information> data = new ArrayList<>();
    int [] icons = {R.drawable.abs,R.drawable.arms,R.drawable.back,R.drawable.chest,R.drawable.full,R.drawable.legs};
    String[] titles={"Abs","Arms","Back","Chest","full","legs"};
    for (int i=0; i<icons.length && i<titles.length;i++){

        Information current = new Information();
        current.images=icons[i];
        current.title=titles[i];
        data.add(current);
    }
    return data;

    }





    }

2 个答案:

答案 0 :(得分:0)

在使用RecyclerView并将创建一个 xml 文件作为列表时,在这种情况下,您必须使用 wrap_content 作为高度否则,滚动时会占用设备的高度。

<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">


<ImageView
   android:id="@+id/listicon"
   android:layout_width="match_parent"
   android:layout_height="220dp />

    </LinearLayout>

在Java中执行此操作:

recycler = (RecyclerView) layout.findViewById(R.id.recycler);
layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recycler.setLayoutManager(layoutManager);
adapter = new CustomAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);

答案 1 :(得分:0)

将其添加到fragment_home.xml中的回收器视图中可以达到目的

// make sure to import rxjs 6 correctly
import { map, switchMap, tap } from 'rxjs/operators';

@Effect()
twoActionEvent = this.actions$
    .ofType(appActions.FIRST_ACTION)
    .pipe(
        switchMap((action$: appActions.firstAction) => {
            return //what ever you need in the first action
        }),
        map((firstActionPayload) => {
            return {
               type: appActions.FIRST_ACTION_SUCCESS,
                payload: firstActionPayload
            }
         }),
         tap(() => {
             this.store.dispatch(new appActions.SecondAction(//payload))
         })
     );
constructor(private actions$: Actions,
            private store: Store<AppState>) {}