本文主要是介绍仿照iphone设置点击更多显示全部内容,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ListView里每一条显示版本信息,由于内容过长只显示部分,展开后能显示全部内容,并且再次点击收缩。
自定义View
public class CollapsableTextView extends LinearLayout implementsView.OnClickListener {/** default text show max lines */private static final int DEFAULT_MAX_LINE_COUNT = 3;private static final int COLLAPSIBLE_STATE_NONE = 0;private static final int COLLAPSIBLE_STATE_SHRINKUP = 1;private static final int COLLAPSIBLE_STATE_SPREAD = 2;private TextView mVersionDetail;private TextView mMoreState;private String shrinkup;private String spread;private int mState = COLLAPSIBLE_STATE_SPREAD;private boolean flag;public CollapsableTextView(Context context, AttributeSet attrs) {super(context, attrs);shrinkup = context.getString(R.string.shrinkup);spread = context.getString(R.string.spread);View view = inflate(context, R.layout.preference_list_item, this);view.setPadding(0, -1, 0, 0);mVersionDetail = (TextView) view.findViewById(R.id.detail);mMoreState = (TextView) view.findViewById(R.id.more);mMoreState.setTextColor(context.getResources().getColor(R.color.holo_blue_dark));mMoreState.setOnClickListener(this);}public CollapsableTextView(Context context) {this(context, null);}public void setDesc(CharSequence charSequence, TextView.BufferType bufferType) {mVersionDetail.setText(charSequence, bufferType);mState = COLLAPSIBLE_STATE_SPREAD;requestLayout();}@Overridepublic void onClick(View v) {flag = !flag;requestLayout();}/**** @param view detail* @return state*/private int checkViewState(TextView view) {Layout layout = view.getLayout();if (layout != null) {int lines = layout.getLineCount();if (lines > 0) {if (layout.getEllipsisCount(lines - 1) == 0) {if (view.getLineCount() <= DEFAULT_MAX_LINE_COUNT) {return COLLAPSIBLE_STATE_NONE;} else {return COLLAPSIBLE_STATE_SPREAD;}}}}return COLLAPSIBLE_STATE_SHRINKUP;}public void updateView() {mState = checkViewState(mVersionDetail);mMoreState.setVisibility(mState > 0 ? View.VISIBLE : View.INVISIBLE);if (mState > 0) {mMoreState.setText(mState == COLLAPSIBLE_STATE_SPREAD ? shrinkup : spread);}}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);updateView();Log.i(ConstantUtil.TAG, "onLayout:" + mVersionDetail.getLineCount() + "," + flag + "," +mMoreState.getVisibility() + "," + checkViewState(mVersionDetail));if (!flag) {flag = true;if (checkViewState(mVersionDetail) == COLLAPSIBLE_STATE_NONE) {mVersionDetail.setMaxLines(DEFAULT_MAX_LINE_COUNT);} else {post(new InnerRunnable());}}}class InnerRunnable implements Runnable {@Overridepublic void run() {Log.i(ConstantUtil.TAG, "Runnable :" + mState);if (mState == COLLAPSIBLE_STATE_SPREAD) {mVersionDetail.setMaxLines(DEFAULT_MAX_LINE_COUNT);mMoreState.setText(spread);mState = COLLAPSIBLE_STATE_SHRINKUP;} else if (mState == COLLAPSIBLE_STATE_SHRINKUP) {mVersionDetail.setMaxLines(Integer.MAX_VALUE);mMoreState.setText(shrinkup);mState = COLLAPSIBLE_STATE_SPREAD;}}}}
Activity页面包含Listview,Adapter由于ListView复用的原因,需要重新requestLayout。
public class PreferenceList extends ListActivity implements AdapterView.OnItemClickListener{PreferenceListAdapter mAdapter;JSONArray mVersionList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mVersionList = getData();getListView().setOnItemClickListener(this);getListView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);mAdapter = new PreferenceListAdapter();setListAdapter(mAdapter);}private JSONArray getData() {JsonHelper helper = new JsonHelper();JSONArray list = null;try {list = helper.getJson(this, ConstantUtil.JSON_FILE_NAME);} catch (JSONException e) {e.printStackTrace();}return list;}@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {view.findViewById(R.id.more).performClick();}private class PreferenceListAdapter extends BaseAdapter {private ViewHolder mHolder;PreferenceListAdapter() {}@Overridepublic int getCount() {return mVersionList.length();}@Overridepublic Object getItem(int position) {Object item = null;try {item = mVersionList.getJSONObject(position);} catch (JSONException e) {e.printStackTrace();}return item;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {mHolder = new ViewHolder();convertView = View.inflate(PreferenceList.this, R.layout.preference_item, null);mHolder.version = (TextView) convertView.findViewById(R.id.version);mHolder.date = (TextView) convertView.findViewById(R.id.date);mHolder.detail = (TextView) convertView.findViewById(R.id.detail);convertView.setTag(mHolder);} else {mHolder = (ViewHolder) convertView.getTag();convertView.requestLayout();}Log.i(ConstantUtil.TAG, "getView: " + position);try {mHolder.version.setText(mVersionList.getJSONObject(position).getString("check_version"));mHolder.date.setText(mVersionList.getJSONObject(position).getString("date"));mHolder.detail.setText(mVersionList.getJSONObject(position).getString("version_detail"));} catch (JSONException e) {e.printStackTrace();}return convertView;}}private class ViewHolder {TextView version;TextView date;TextView detail;}}
布局文件比较简单,就看自定义布局
<com.example.zxz.asdemo.data.CollapsableTextViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="vertical"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"></com.example.zxz.asdemo.data.CollapsableTextView>
android:textAppearance="?android:attr/textAppearanceSmall"
字体样式
这篇关于仿照iphone设置点击更多显示全部内容的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!