Recycler视图不渲染视图

时间:2017-08-01 21:02:48

标签: android android-recyclerview adapter android-arrayadapter

我没有从回收站获得任何视图,我做了一些调试,并且在列表中调用了适配器的构造函数,也使用了方法

  

getItemCount()

但其余的似乎都没有被执行。 我一直在堆栈里徘徊寻找这个问题,我找到了解决方案的不同方法,问题对我来说没有问题,也许我只是错过了一些我看不到的东西。

我在某处读到可能是因为回收者视图在滚动视图中,但给出的解决方案对我没有用

我的自定义适配器:

public class CircumstancesAdapter extends RecyclerView.Adapter<CircumstancesAdapter.CircumstancesViewHolder> {
List<CatalogRespModel> circumList;
Context mContext;

public CircumstancesAdapter(List<CatalogRespModel> list, Context context) {
    this.circumList = list;
    this.mContext = context;
}

@Override
public CircumstancesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.row_item_circumstances, parent, false);
    return new CircumstancesViewHolder(view);
}

@Override
public void onBindViewHolder(CircumstancesViewHolder holder, int position) {
    holder.tvCircum.setText(circumList.get(position).get_Descripcion());
}

@Override
public int getItemCount() {
    if(circumList != null) return circumList.size();
    else return 0;
}

public  class CircumstancesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    RadioButton rbCircum;
    TextView tvCircum;
    private Context context;

    public CircumstancesViewHolder(View itemView) {
        super(itemView);
        rbCircum = (RadioButton) itemView.findViewById(R.id.rb_circ);
        rbCircum.setOnClickListener(this);
        tvCircum = (TextView) itemView.findViewById(R.id.tv_circ);
        this.context = itemView.getContext();
    }

    @Override
    public void onClick(View v) {

    }
}

}

我的容器布局:

<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">

<!-- TODO: Update blank fragment layout -->

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="15dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="20dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="8dp"
                android:id="@+id/text2"
                android:text="@string/text_circ"
                android:textSize="20sp"
                android:textColor="@color/colorPrimaryshadow"/>

            <RelativeLayout
                android:id="@+id/placa"
                android:layout_width="180dp"
                android:layout_height="120dp"
                android:layout_gravity="center"
                android:background="@drawable/placa">

                    <TextView
                        android:id="@+id/tv_plate"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_vertical|center_horizontal"
                        android:layout_centerInParent="true"
                        android:text="@string/dummy"
                        android:textSize="40sp"
                        android:textColor="@android:color/black"/>

            </RelativeLayout>

            <ImageView
                android:id="@+id/iv_indicator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:clickable="true"
                android:src="@drawable/conductora" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rview_circumstances"
                android:layout_width="match_parent"
                android:background="#fff"
                android:orientation="vertical"
                android:layout_height="wrap_content"/>

            <Button
                android:id="@+id/boton_continuar_circustanciasa"
                android:layout_marginTop="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/continuar"
                android:layout_marginBottom="10dp"
                android:elevation="30dp"
                android:layout_gravity="center"/>


        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>
</FrameLayout>

我的行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="#fff"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:layout_height="wrap_content">

<RadioButton
    android:id="@+id/rb_circ"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/colorAccentDark"
    android:gravity="center_vertical"
    android:textSize="14sp" />

<TextView
    android:layout_gravity="center_vertical"
    android:gravity="center_vertical"
    android:id="@+id/tv_circ"
    android:textSize="14sp"
    android:text="texto de prueba"
    android:textColor="@color/colorAccentDark"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
  </LinearLayout>

并在片段内的onCreateView中:

recyclerView = (RecyclerView) rootView.findViewById(R.id.rview_circumstances);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setHasFixedSize(true);
if(Catalogs.circumstancesArray.size() > 0){
        adapter = new CircumstancesAdapter(Catalogs.circumstancesArray, getContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);

    }

注意:我检查了列表大小并且大于0

请求的片段代码:

public class CircumstancesFragment extends Fragment {

Button btnContinue;
TextView tvPlate;
private ImageView ivIndicator;
private static int iNumDevices;
IFragmentListener iFragmentListener;
private RecyclerView recyclerView;
RecyclerView.Adapter adapter;

private final static String TAGMap = CircumstancesFragment.class.getSimpleName();
public static final String TAG = "CircumstancesFragment.java";

public static CircumstancesFragment getInstance(Bundle bundle){
    if(bundle != null){
        iNumDevices = bundle.getInt("numDevices");
    }
    return new CircumstancesFragment();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if(context != null){
        if(context instanceof IFragmentListener){
            iFragmentListener = (IFragmentListener) context;
        } else
            throw new RuntimeException("el contexto no esta implementando la interfaz");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View rootView = inflater.inflate(R.layout.fragment_circustancias_, container, false);
    btnContinue = (Button) rootView.findViewById(R.id.boton_continuar_circustanciasa);
    tvPlate = (TextView) rootView.findViewById(R.id.tv_plate);
    recyclerView = (RecyclerView) rootView.findViewById(R.id.rview_circumstances);
    ivIndicator = (ImageView) rootView.findViewById(R.id.iv_indicator);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setHasFixedSize(true);
    if(Catalogs.circumstancesArray.size() > 0){
        adapter = new CircumstancesAdapter(Catalogs.circumstancesArray, getContext());
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }
    btnContinue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view)

        {
            try
            {
                iFragmentListener.notify(null, TAG);
            } catch (Exception ex)
            {
                Log.d("error", ex.getMessage());
            }
        }
    });
    setLayout(iNumDevices);
    return rootView;
}

private void setLayout(int i){
    tvPlate.setText(FdccoreConstants.insuredArray.get(0).get_Placa_Vehiculo());
    if(i== FdccoreConstants.ONE_DEVICE){
        ivIndicator.setImageResource(R.drawable.conductora);
    }else if(i== FdccoreConstants.TWO_DEVICE){
        ivIndicator.setImageResource(R.drawable.only_conductor);
    }
}

private void resetView(){

}
}

2 个答案:

答案 0 :(得分:0)

请提供更多代码,因为您的布局和适配器工作正常。

我只将您的模型列表更改为String。

当您检查列表大小和list.size()&gt;时,可能就位了0添加

 adapter.notifyDataSetChanged();

=====

 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rview_circumstances);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(true);

        list = new ArrayList<>();

        list.add("asd");
        list.add("sasf");
        list.add("asasfasf");
        list.add("asgagsad");
        list.add("asgas");
        list.add("asagasgd");
        list.add("asreyeryd");
        list.add("asgsheyd");

        CircumstancesAdapter adapter = new CircumstancesAdapter(list, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

enter image description here

答案 1 :(得分:-1)

解决方案:为了使用滚动视图,我必须添加此行

  

编译'com.android.support:recyclerview-v7:25.3.1'

仅编译appcompat-v7无法正常工作