为什么RecyclerView不会在设备上显示但在Layout Inspector中可见?

时间:2017-05-02 00:58:05

标签: android android-recyclerview layout-inspector

我想在我的应用程序上显示带有图片的幻灯片,我实现了一个水平的RecyclerView。

左边的屏幕是右侧同一屏幕的运行时屏幕截图,在我将照片添加到recyclerview后,在布局检查器中显示

SS on phone SS on LI

RecyclerView的原始位置在CardView内部,我把它移出去了,因为它也没有显示出来。有什么想法吗?

一些代码:

activity.xml

<FrameLayout 
    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="match_parent"
    tools:context=".TheActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="TheFragment"
        android:tag="@string/the_tag"
        tools:layout="@layout/the_layout"
        android:id="@+id/the_id"/>
</FrameLayout>

fragment.xml之

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:bind="http://schemas.android.com/apk/res-auto"
    tools:context="TheActivity" >

<data>
    <!-- Some vars -->
</data>

<LinearLayout android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView  android:visibility="visible"
        android:id="@+id/frag_add_property_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:orientation="vertical"
            android:focusableInTouchMode="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">               
            <android.support.v7.widget.RecyclerView android:id="@+id/the_rv"
                android:layout_width="match_parent"
                android:layout_height="150dp"
     app:layoutManager="android.support.v7.widget.LinearLayoutManager"                    
                android:orientation="horizontal"
  android:layout_marginBottom="@dimen/activity_half_vertical_margin"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

public class TheFragment extends BaseFragment implements TheView { 

    private PictureFilesAdapter adapter;
    @BindView(R.id.rv_add_property_pics) 
    RecyclerView imageRv;

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        bind = DataBindingUtil.inflate(inflater, getLayout(), container, false);
        unbinder = ButterKnife.bind(this, bind.getRoot());

        Drawable dividerDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.rv_item_hor_divider);
        imageRv.addItemDecoration(new CustomItemDecoration(dividerDrawable));

        return bind.getRoot();
    }

class PictureFilesAdapter extends RecyclerView.Adapter<PropertyPicturesViewHolder> {

    Context context;
    public ArrayList<File> files;
    public ArrayList<File> getFiles() {
        return files;
    }

    AddPropertyView view;

    PictureFilesAdapter(AddPropertyView view, Context context) {
        this.context = context;
        this.files = new ArrayList<>();
        this.view = view;
    }

    @Override
    public PropertyPicturesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.item_rv_add_property, parent, false);
        return new AddPropertyFragment.PropertyPicturesViewHolder(v);
    }

    @Override
    public void onBindViewHolder(PropertyPicturesViewHolder holder, int position) {
        holder.textView.setText(files.get(position).getAbsolutePath());

        Picasso.with(context)
                .load(files.get(position))
                .into(holder.imageView);
    }

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

    void addFile(File file) {
        this.files.add(file);
        notifyDataSetChanged();
    }

}

class PropertyPicturesViewHolder extends RecyclerView.ViewHolder {

    private Uri uri;
    @BindView(R.id.item_rv_add_prop_image) ImageView imageView;
    @BindView(R.id.item_rv_add_prop_image_src) TextView textView;

    PropertyPicturesViewHolder(View view) {
        super(view);

        ButterKnife.bind(this, view);
        view.setOnClickListener((v) -> {
            // some logic
        });
    }
}

}

2 个答案:

答案 0 :(得分:1)

事实证明,即使是毕加索,位图也太大了。在显示位图之前缩小位图确实是神奇的。

答案 1 :(得分:-1)

你能提供你的代码吗?

有几种可能性,它没有出现。

  1. 它没有布局管理器
  2. 没有适配器
  3. adapter.getCount()返回0