RecyclerView项目布局未动态刷新或更新

时间:2017-11-28 22:38:18

标签: android android-layout android-recyclerview

我正在努力要求在RecyclerView中显示2个文本视图组合为Max 3行,如果超过我必须显示"显示更多"文本,以便用户可以单击并查看更多信息。

当我将可见性或setMaxLines更改为TextView时,它不会立即反映在RecyclerView中。但是当我滚动到底部并返回时,布局变化正在反映。

要获取实际文本长度,我使用的是View.addOnLayoutChangeListener()回调。

有人可以查看我的代码并帮我解决这个问题吗?

视频链接:: https://vimeo.com/244914225

MainActivity Code:

public class MainActivity extends AppCompatActivity {


public static final int MAX_LINE_COUNT = 3;

private RecyclerViewAdapter adapter;
private RecyclerView rv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] itemList = getResources().getStringArray(R.array.itemList);

    rv = findViewById(R.id.rv);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,
            LinearLayoutManager.VERTICAL, false);
    rv.setLayoutManager(layoutManager);
    adapter = new RecyclerViewAdapter(Arrays.asList(itemList));
    rv.setAdapter(adapter);

}

public class RecyclerViewAdapter extends RecyclerView.Adapter<ItemViewHolder> {

    private final List<String> itemList;

    public RecyclerViewAdapter(List<String> itemList) {
        this.itemList = itemList;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_view,
                viewGroup, false);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        holder.bindData(itemList.get(position), position);
    }

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

private class ItemViewHolder extends RecyclerView.ViewHolder {

    private final TextView subject;
    private final TextView details;
    private final TextView seeMore;

    public ItemViewHolder(View itemView) {
        super(itemView);
        subject = itemView.findViewById(R.id.tv1);
        details = itemView.findViewById(R.id.tv2);
        seeMore = itemView.findViewById(R.id.tv3);
    }

    public void bindData(final String data, int position) {
        subject.setText(data);
        details.setText(data);
        seeMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();
            }
        });
        getTv1LineCount();
    }

    private void getTv1LineCount() {
        subject.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
                int lineCount = subject.getLineCount();
                subject.setMaxLines(MAX_LINE_COUNT);
                if (lineCount == MAX_LINE_COUNT) {
                    getTv2LineCount(lineCount);
                } else if (lineCount > MAX_LINE_COUNT) {
                    details.setVisibility(View.GONE);
                    seeMore.setVisibility(View.VISIBLE);
                } else {
                    if (TextUtils.isEmpty(details.getText().toString())) {
                        details.setVisibility(View.GONE);
                        seeMore.setVisibility(View.GONE);
                    } else
                        getTv2LineCount(lineCount);
                }
            }
        });
    }

    private void getTv2LineCount(int tv1LineCount) {
        final int maxLineCount = MAX_LINE_COUNT - tv1LineCount;
        details.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
                int lineCount = details.getLineCount();
                details.setMaxLines(maxLineCount);
                if (lineCount > maxLineCount) {
                    seeMore.setVisibility(View.VISIBLE);
                } else {
                    seeMore.setVisibility(View.GONE);
                }
            }
        });
    }
}

}

ItemView xml文件

<android.support.v7.widget.CardView 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="@color/itemBgColor">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample Text view 1"
        android:textSize="18dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sample Text view 2"
        android:textSize="16dp" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="See More"
        android:textColor="#0066cc" />

</LinearLayout>

0 个答案:

没有答案