文字选择无法在自定义列表视图Android中使用

时间:2016-07-20 17:21:13

标签: android listview android-fragments

我在ListView片段中有三个TextViews组成的自定义ViewPager,我正在尝试使TextViews中的文本可选。当我在XML中设置isSelectable=true属性时,屏幕顶部的选择文本栏会显示片刻并消失。发生这种情况时,Android Monitor的输出是:

  

D / dalvikvm:GC_FOR_ALLOC释放2326K,21%免费20756K / 26252K,暂停   46毫秒,总计46毫秒我/编舞:跳过37帧!应用程序   可能在其主线上做了太多工作。

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="2dp"
    android:paddingTop="10dp">

    <TextView
        android:id="@+id/call_dt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:textIsSelectable="true"


        />

    <TextView
        android:id="@+id/call_tm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_toRightOf="@+id/call_dt"
      android:textIsSelectable="false"
        android:paddingBottom="3dp"
        android:paddingLeft="5dp"

        android:textColorLink="@android:color/holo_blue_dark"
        />

    <TextView
        android:id="@+id/call_notes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/call_dt"
       android:textIsSelectable="false"
        android:paddingBottom="3dp"
        android:text="New Text" />


</RelativeLayout>

以下是带有自定义ListView适配器的片段:

/**
 * Created by Steve on 7/14/2016.
 */
public class EmailAccountsFragment extends Fragment {
    private ArrayList resultsListItems;
    private ListView listView;

    public static EmailAccountsFragment newInstance(int sectionNumber) {
        EmailAccountsFragment fragment = new EmailAccountsFragment();

        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragments_list, container, false);


        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.frag_list);
        resultsListItems = (ArrayList) ResultsAccess.getEmailAcctResultsList();

        EmailAcctAdapter mAdapter = new EmailAcctAdapter(EmailAccountsFragment.this.getContext(), R.layout.email_accts_item, resultsListItems);
        listView.setAdapter(mAdapter);
    }


    class EmailAcctAdapter extends ArrayAdapter<Result> {

        private Context mContext;

        public EmailAcctAdapter(Context context, int textViewResourceId, List<Result> objects) {
            super(context, textViewResourceId, objects);
            mContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            return getViewOptimize(position, convertView, parent);
        }

        public View getViewOptimize(int position, View convertView, ViewGroup parent) {
            Result result = getItem(position);

            View row = convertView;
            ViewHolder viewHolder = null;
            if (row == null) {
                LayoutInflater inflater = (LayoutInflater)
                        mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(R.layout.email_accts_item, parent, false);
                viewHolder = new ViewHolder();
                viewHolder.altEmail = (TextView) row.findViewById(R.id.alt_email);
                viewHolder.os_account = (TextView) row.findViewById(R.id.os_account);
                viewHolder.milasodit = (TextView) row.findViewById(R.id.milasodit);
                viewHolder.start_dt = (TextView) row.findViewById(R.id.start_dt);
                viewHolder.end_dt = (TextView) row.findViewById(R.id.end_dt);
                viewHolder.spamblock_yn = (TextView) row.findViewById(R.id.spamblock_yn);
                viewHolder.spamnotify_yn = (TextView) row.findViewById(R.id.spamnotify_yn);
                row.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) row.getTag();
            }
            setTextViewsText(viewHolder, result);
            return row;
        }

        class ViewHolder {


            public TextView altEmail,
                    os_account,
                    milasodit,
                    start_dt,
                    end_dt,
                    spamblock_yn,
                    spamnotify_yn;

        }

        public void setTextViewsText(ViewHolder v, Result r) {

            if (notNull(r.altEmail)) {
                v.altEmail.setText(r.altEmail);
                v.altEmail.setVisibility(View.VISIBLE);
            } else {
                v.altEmail.setVisibility(View.GONE);

            }

            if (notNull(r.os_account)) {
                v.os_account.setText("Account: " + r.os_account);
                v.os_account.setVisibility(View.VISIBLE);

            } else {
                v.os_account.setVisibility(View.GONE);
            }
            if (notNull(r.milasodit)) {
                String slash = r.os_account == null ? "" : "/";
                v.milasodit.setText(slash + r.milasodit);
                v.milasodit.setVisibility(View.VISIBLE);
            } else {
                v.milasodit.setVisibility(View.GONE);

            }
            if (notNull(r.start_dt)) {
                v.start_dt.setText("Start: " + r.start_dt);
                v.start_dt.setVisibility(View.VISIBLE);

            } else {
                v.start_dt.setVisibility(View.GONE);

            }
            if (notNull(r.end_dt)) {

                v.end_dt.setText("End: " + r.end_dt);
                v.end_dt.setVisibility(View.VISIBLE);

            } else {
                v.end_dt.setVisibility(View.GONE);

            }
            if (notNull(r.spamblock_yn)) {
                v.spamblock_yn.setText("SpamBlock: " + r.spamblock_yn);
                v.spamblock_yn.setVisibility(View.VISIBLE);

            } else {
                v.spamblock_yn.setVisibility(View.GONE);

            }
            if (notNull(r.spamnotify_yn)) {
                v.spamnotify_yn.setText("SpamNotify: " + r.spamnotify_yn);
                v.spamblock_yn.setVisibility(View.VISIBLE);
            } else {
                v.spamnotify_yn.setVisibility(View.GONE);


            }

        }

        private boolean notNull(String field) {
            return field != null;
        }

    }
}

我在另一个标签/片段中也有类似的ListView。第三个片段只有3个TextView,没有ListView,选择工作。我在实施中使用了太多内存,还是另一个问题?如果是这样,我怎样才能更好地实现这个?

0 个答案:

没有答案