在Recyclerview

时间:2016-08-27 13:43:22

标签: android android-activity android-recyclerview

我的应用程序中有一个RecyclerView,由listadapter填充。但有些元素在滚动后会丢失,甚至在开始时都不会显示出来。在这里,我在我的Activity中调用ListAdapter:

ListAdapter adapter = new ListAdapter(posts, new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            final PostViewHolder holder = new PostViewHolder(v);
                            final Intent intent = new Intent(MainActivity.this, PostViewActivity.class);
                            intent.setAction(Intent.ACTION_SEND);
                            intent.setType("image/png");
                            final int type = Integer.parseInt((String) holder.cardView.getTag(R.string.ident_type));
                            Log.e("TYPE", "" + type);
                            final String username = (String) holder.cardView.getTag(R.string.ident_username);

                            if (type == 2) {

                                holder.imagePost.setTransitionName(getString(R.string.transition_image));
                                PostViewActivity post;

                                int postWidth = 0;
                                int postHeight = 0;
                                postWidth = holder.imagePost.getWidth();
                                postHeight = holder.imagePost.getHeight();
                                Drawable drawable = holder.imagePost.getDrawable();
                                Bitmap photo = ((BitmapDrawable) drawable).getBitmap();
                                String path = MediaStore.Images.Media.insertImage(getContentResolver(), photo, "pic", null);
                                Uri uri = Uri.parse(path);
                                intent.putExtra(Intent.EXTRA_STREAM, uri);
                                intent.putExtra("dim", postWidth + "/" + postHeight);

                            } else {

                            }
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    ActivityOptionsCompat options;
                                    Pair<View, String> imagePair;
                                    if (type == 2) {
                                        imagePair = Pair.create((View) holder.imagePost, getString(R.string.transition_image));
                                        options = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, imagePair);
                                    } else {
                                        options = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this);
                                    }
                                    String id = (String) holder.cardView.getTag(R.string.ident_id);
                                    intent.putExtra("type", type);
                                    intent.putExtra("username", username);
                                    intent.putExtra("id", id);
                                    startActivity(intent, options.toBundle());
                                }
                            });


                        }
                    }).start();
                }
            }, width, height, context, activity);
            RecyclerView rv = (RecyclerView) findViewById(R.id.user_interface_recycler);
            StaggeredGridLayoutManager lm = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
            rv.setLayoutManager(lm);
            rv.setAdapter(adapter);

以下是listadapter本身:

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PostViewHolder> {
List<Post> posts;
View.OnClickListener listener;
int width;
int height;
private Context context;
public static final String URL = "http://" + Connection.HOSTNAME + "/story/images/";
MainActivity activity;

public ListAdapter(List<Post> posts, View.OnClickListener listener, int width, int height, Context context, MainActivity activity) {
    this.posts = posts;
    this.listener = listener;
    this.width = width;
    this.height = height;
    this.context = context;
    this.activity = activity;
}

int placeholder = 0;

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

@Override
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.insert_layout, viewGroup, false);
    PostViewHolder pvh = new PostViewHolder(v);
    return pvh;
}

@Override
public void onBindViewHolder(final PostViewHolder postViewHolder, int i) {
    Log.e("NUMBER" + i, posts.get(i).getType()+"");
    switch (posts.get(i).getType()) {
        case 1:
            postViewHolder.layoutPhoto.setVisibility(View.GONE);
            textPost(postViewHolder, i);
            break;
        case 2:
            postViewHolder.layoutText.setVisibility(View.GONE);
            imagePost(postViewHolder, i);
            break;
    }
}

private void imagePost(final PostViewHolder postViewHolder, final int i) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    new Thread(new Runnable() {
        @Override
        public void run() {
            String mainText = posts.get(i).getText();
            if (mainText.length() > 100 && posts.get(i).getType() == 2) {
                mainText = mainText.substring(0, 100);
                mainText += "(...)";
            }


            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(posts.get(i).getTime());
            if (posts.get(i).getTime() > MainActivity.minTime) {
                MainActivity.minTime = posts.get(i).getTime();
            }
            int mYear = calendar.get(Calendar.YEAR);
            int mMonth = calendar.get(Calendar.MONTH);
            int mDay = calendar.get(Calendar.DAY_OF_MONTH);
            int mMin = calendar.get(Calendar.MINUTE);
            int mHour = calendar.get(Calendar.HOUR);
            String mmin;
            if (mMin < 10) {
                mmin = 0 + "" + mMin;
            } else {
                mmin = Integer.toString(mMin);
            }
            String mhour;
            if (mHour < 10) {
                mhour = 0 + "" + mHour;
            } else {
                mhour = Integer.toString(mHour);
            }
            String resultdate = mDay + "." + (mMonth + 1) + "." + mYear + " " + mhour + ":" + mmin;
            String storyName = posts.get(i).getStory().getName();
            String insertText = posts.get(i).getText() + " by " + posts.get(i).getUsername() + "\n" + resultdate + "\n " + storyName;
            final Spannable spannable = new SpannableString(insertText);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorAccent)), (posts.get(i).getText() + " by ").length(), insertText.length() - resultdate.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorDezent)), posts.get(i).getText().length(), (posts.get(i).getText() + " by ").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new RelativeSizeSpan(1f), (posts.get(i).getText() + " by " + posts.get(i).getUsername() + "\n").length(), insertText.length() - storyName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorDezent)), (posts.get(i).getText() + " by " + posts.get(i).getUsername() + "\n").length(), insertText.length() - storyName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorPrimaryDark)), insertText.length() - storyName.length(), insertText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.e("photo", "photo");
                    postViewHolder.cv.setTag(R.string.ident_type, Integer.toString(posts.get(i).getType()));
                    postViewHolder.cv.setTag(R.string.ident_id, Integer.toString(posts.get(i).getID()));
                    int imgWidth = (int) ((float) width / 6);
                    int imgHeight = (int) ((float) width / 6);
                    Picasso.with(context).load(ListAdapter.URL + posts.get(i).getImageUser() + ".png").transform(new CircleTransform()).resize(imgWidth, imgHeight).centerCrop().into(postViewHolder.photoUserPhoto);
                    Picasso.with(context).load(ListAdapter.URL + posts.get(i).getImagePost() + ".png").resize(width, 500).centerCrop().into(postViewHolder.photoPostPhoto);
                    postViewHolder.photoText.setText(spannable);
                    postViewHolder.photoPostPhoto.setTransitionName("");
                    postViewHolder.itemView.setOnClickListener(listener);
                    postViewHolder.cv.setTag(R.string.ident_username, posts.get(i).getUsername());
                }
            });

        }
    }

    ).start();

}

private void textPost(final PostViewHolder postViewHolder, final int i) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            String mainText = posts.get(i).getText();
            Long postdate = posts.get(i).getTime();
            String username = posts.get(i).getUsername();
            //Bitmap userPhoto = posts.get(i).getImageUser();


            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(posts.get(i).getTime());
            if (posts.get(i).getTime() > MainActivity.minTime) {
                MainActivity.minTime = posts.get(i).getTime();
            }

            int mYear = calendar.get(Calendar.YEAR);
            int mMonth = calendar.get(Calendar.MONTH);
            int mDay = calendar.get(Calendar.DAY_OF_MONTH);
            int mMin = calendar.get(Calendar.MINUTE);
            int mHour = calendar.get(Calendar.HOUR);
            String mmin;
            if (mMin < 10) {
                mmin = 0 + "" + mMin;
            } else {
                mmin = Integer.toString(mMin);
            }
            String mhour;
            if (mHour < 10) {
                mhour = 0 + "" + mHour;
            } else {
                mhour = Integer.toString(mHour);
            }
            String resultdate = mDay + "." + (mMonth + 1) + "." + mYear + " " + mhour + ":" + mmin;
            String storyName = posts.get(i).getStory().getName();
            String insertText = posts.get(i).getText() + " by " + posts.get(i).getUsername() + "\n" + resultdate + "\n " + storyName;
            final Spannable spannable = new SpannableString(insertText);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorAccent)), (posts.get(i).getText() + " by ").length(), insertText.length() - resultdate.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorDezent)), posts.get(i).getText().length(), (posts.get(i).getText() + " by ").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new RelativeSizeSpan(0.8f), (posts.get(i).getText() + " by " + posts.get(i).getUsername() + "\n").length(), insertText.length() - storyName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorDezent)), (posts.get(i).getText() + " by " + posts.get(i).getUsername() + "\n").length(), insertText.length() - storyName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannable.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.colorPrimaryDark)), insertText.length() - storyName.length(), insertText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Log.e("text", "text");
                    postViewHolder.textUser.setText(spannable);
                    int imgWidth = (int) ((float) width / 6);
                    int imgHeight = (int) ((float) width / 6);
                    Picasso.with(context).load(ListAdapter.URL + posts.get(i).getImageUser() + ".png").transform(new CircleTransform()).resize(imgWidth, imgHeight).centerCrop().into(postViewHolder.textUserPhoto);
                    postViewHolder.itemView.setOnClickListener(listener);
                    postViewHolder.cv.setTag(R.string.ident_type, Integer.toString(posts.get(i).getType()));
                    postViewHolder.cv.setTag(R.string.ident_id, Integer.toString(posts.get(i).getID()));
                    postViewHolder.cv.setTag(R.string.ident_username, posts.get(i).getUsername());
                }
            });
        }
    }).start();

}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}


public class PostViewHolder extends RecyclerView.ViewHolder {
    CardView cv;

    LinearLayout layoutPhoto;
    TextView photoText;
    ImageView photoPostPhoto;
    ImageView photoUserPhoto;

    LinearLayout layoutText;
    TextView textUser;
    ImageView textUserPhoto;

    PostViewHolder(View itemView) {
        super(itemView);
        cv = (CardView) itemView.findViewById(R.id.cv);

        photoText = (TextView) itemView.findViewById(R.id.photo_user_text);
        photoPostPhoto = (ImageView) itemView.findViewById(R.id.photo_postPhoto);
        layoutPhoto = (LinearLayout) itemView.findViewById(R.id.insert_layout_photo);
        photoUserPhoto = (ImageView) itemView.findViewById(R.id.photo_username_photo);

        layoutText = (LinearLayout) itemView.findViewById(R.id.insert_layout_text);
        textUser = (TextView) itemView.findViewById(R.id.text_user_text);
        textUserPhoto = (ImageView) itemView.findViewById(R.id.text_username_photo);
    }

}

}

问题出在哪里?谢谢!

编辑:

fragment_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/activity_main_swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/post_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    </android.support.v7.widget.RecyclerView>


</android.support.v4.widget.SwipeRefreshLayout>

activity_main(这是一个coordinatorLayout,但这会被切掉。不知道为什么):

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways|snap">
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

2 个答案:

答案 0 :(得分:1)

在onBindViewHolder中,您将视图的一部分设置为GONE。您应该确保所有必要的部件都是可见的。试试这个(常量对于像这样的开关块的可读性是好的,特别是当它们变大时):

private static final int TEXT_POST = 1;
private static final int IMAGE_POST = 2;

@Override
public void onBindViewHolder(final PostViewHolder postViewHolder, int i) {
    Log.e("NUMBER" + i, posts.get(i).getType()+"");
    switch (posts.get(i).getType()) {
        case TEXT_POST:
            postViewHolder.layoutText.setVisibility(View.VISIBLE);
            postViewHolder.layoutPhoto.setVisibility(View.GONE);
            textPost(postViewHolder, i);
            break;
        case IMAGE_POST:
            postViewHolder.layoutText.setVisibility(View.GONE);
            postViewHolder.layoutPhoto.setVisibility(View.VISIBLE);
            imagePost(postViewHolder, i);
            break;
        default:
            // probably unnecessary      
            postViewHolder.layoutText.setVisibility(View.VISIBLE);
            postViewHolder.layoutPhoto.setVisibility(View.VISIBLE);
    }
}

答案 1 :(得分:0)

在依赖项中使用最新的recyclerview版本,然后在xml文件中使用&#34; wrap_content&#34; 作为其高度