天天看点

[Android]自定义可展开的ExpandTextView[Android]自定义可展开的ExpandTextView

[Android]自定义可展开的ExpandTextView

@Author GQ 年月日   

可以在ScrollView、ListView中使用
adapter可以自定义,用于多行文字展示
           

原文github地址

效果图

[Android]自定义可展开的ExpandTextView[Android]自定义可展开的ExpandTextView
  • Android自定义可展开的ExpandTextView
    • 效果图
    • AndroidStudio使用
    • 使用

1. AndroidStudio使用

dependencies {

    compile 'com.ms-square:expandableTextView:0.1.4'

}
           

2. 使用

  • Java
//ListView中使用
 private ListView lv;
 private SampleTextListAdapter adapter;

 lv = (ListView) findViewById(R.id.lv);
 adapter = new SampleTextListAdapter(this);
 lv.setAdapter(adapter);
           
  • SampleTextListAdapter
public class SampleTextListAdapter extends BaseAdapter {

    private final Context mContext;
    private final SparseBooleanArray mCollapsedStatus;
    private final String[] sampleStrings;

    public SampleTextListAdapter(Context context) {
        mContext  = context;
        mCollapsedStatus = new SparseBooleanArray();
        sampleStrings = mContext.getResources().getStringArray(R.array.sampleStrings);
    }

    @Override
    public int getCount() {
        return sampleStrings.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return ;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.expandableTextView = (ExpandableTextView) convertView.findViewById(R.id.expand_text_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.expandableTextView.setText(sampleStrings[position], mCollapsedStatus, position);

        return convertView;
    }


    private static class ViewHolder{
        ExpandableTextView expandableTextView;
    }
}
           
  • list_item.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:expandableTextView="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expand_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        expandableTextView:animAlphaStart="1"
        expandableTextView:maxCollapsedLines="4">

        <TextView
            android:id="@id/expandable_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="8dp"
            android:fontFamily="sans-serif-light"
            android:textColor="#666666"
            android:textSize="16sp"/>

        <ImageButton
            android:id="@id/expand_collapse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:background="@android:color/transparent"
            android:padding="16dp"/>
    </com.ms.square.android.expandabletextview.ExpandableTextView>

</LinearLayout>
           
  • Java
//ScrollView中使用
private LinearLayout sample1, sample2;

 sample1 = (LinearLayout) findViewById(R.id.sample1);
 sample2 = (LinearLayout) findViewById(R.id.sample2);
 ExpandableTextView expTv1 = (ExpandableTextView) sample1.findViewById(R.id.expand_text_view);
 ExpandableTextView expTv2 = (ExpandableTextView) sample2.findViewById(R.id.expand_text_view);

 expTv1.setOnExpandStateChangeListener(new ExpandableTextView.OnExpandStateChangeListener() {
            @Override
            public void onExpandStateChanged(TextView textView, boolean isExpanded) {
                Toast.makeText(MainActivity.this, isExpanded ? "Expanded" : "Collapsed", Toast.LENGTH_SHORT).show();
            }
        });

expTv1.setText(getString(R.string.dummy_text1));
expTv2.setText(getString(R.string.dummy_text2));
           
  • ScrollView中使用XML
<ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <include
                android:id="@+id/sample1"
                layout="@layout/text_item" />

            <include
                android:id="@+id/sample2"
                layout="@layout/text_item" />
        </LinearLayout>
    </ScrollView>
           
  • ScrollView中的text_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:expandableTextView="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_marginLeft="10dp"
        android:gravity="bottom"
        android:singleLine="true"
        android:textColor="#333333"
        android:textSize="18sp"/>

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expand_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        expandableTextView:maxCollapsedLines="4">

        <TextView
            android:id="@id/expandable_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="8dp"
            android:fontFamily="sans-serif-light"
            android:textColor="#666666"
            android:textSize="16sp"/>

        <ImageButton
            android:id="@id/expand_collapse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|bottom"
            android:background="@android:color/transparent"
            android:padding="16dp"/>
    </com.ms.square.android.expandabletextview.ExpandableTextView>

</LinearLayout>