如何将多个触摸动作一个单独的列表项? [英] How to attach multiple touch actions to a single list item?

查看:158
本文介绍了如何将多个触摸动作一个单独的列表项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是下面的列表布局的项目的有关联的评论的。的数量的注释的由右侧的框表示。

I am using the following list layout for items that have associated comments. The number of comments are indicated by the box on the right side.

目前,我现在用的是 onListItemClick 处理程序启动其他的详细信息的看法。

Currently, I am using the onListItemClick handler to launch another details view.

public class CustomListFragment extends ListFragment
                                implements LoaderCallbacks<Cursor> {

    private Activity mActivity;
    private CursorAdapter mAdapter;
    private QueryParameters mQueryParameters;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setEmptyText("No data to display");

        mActivity = getActivity();
        if (mActivity == null)  {
            Log.e(getClass().getName(), "Activity is null");
            return;
        }
        // Prepare query.
        mQueryParameters = QueryHelper.getQueryParameters(mActivity);
        if (mQueryParameters == null || !mQueryParameters.isPreparedForLoader()) {
            Log.d(getClass().getName(), "One or more query parameters are null.");
            return;
        }
        mAdapter = new CustomCursorAdapter(mActivity, null, 0);
        setListAdapter(mAdapter);
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle extras) {
        return new CursorLoader(mActivity,
                mQueryParameters.uri,
                mQueryParameters.fromColumnsLoader,
                mQueryParameters.selection,
                mQueryParameters.selectionArgs,
                mQueryParameters.sortOrder);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.swapCursor(cursor);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(mActivity, ItemDetailsActivity.class);
        intent.putExtra("ITEM_URI", mItemUri);
        mActivity.startActivity(intent);
    }
}

我想实现,用户可以触摸右侧的框元素。那么这个动作应该启动相关的 CommentsActivity 。当用户触摸左侧,但是,它仍然应该启动 ItemDetailsActivity
在这两种情况下,我需要通过 itemUri 的意图,这样无论是的详细信息的视图或评论列表对于特定的项目的可装载。
ListFragment 使用的CursorAdapter 绑定的TextView s到的属性的项目的。

I would like to implement that the user can touch the box element on the right. This action should then start the associated CommentsActivity. When the user touches the left side, however, it should still launch the ItemDetailsActivity.
In both cases, I need to pass the itemUri with the intent so that either the details view or the comments list for the particular item can be loaded.
The ListFragment uses a CursorAdapter to bind the TextViews to the properties of an item.

问:

  • 我如何可以将第二触摸动作一个单独的列表项?

推荐答案

您还没有发布相关的适配器本身的任何code,但我发现你的 previous问题以及你最那里的方式。

You hadn't posted any code relating to the adapter itself, but I found your previous question and you are most of the way there.

bindView(),让我们修改 comments_count 的TextView保存当前行的索引标签(你的 itemUri ),并添加一个简单的OnClickListener:

In bindView(), let's modify your comments_count TextView to save the index of the current row in the tag (for your itemUri) and add a simple OnClickListener:

public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder)view.getTag();
    if (holder == null) {
        ...
        holder.comments_count.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Get the position from the ViewHolder
                long id = (Long) v.getTag();
                Toast.makeText(v.getContext(), "Comment Click: " + id, Toast.LENGTH_SHORT).show();
            }
        });
    }
    ...
    holder.comments_count.setTag(cursor.getLong(0));
}

当用户点击了排它仍然呼吁 onListItemClick(),但如果他们点击评论框。评论栏中触发上面,你可以将用户引导到你的 CommentsActivity 的OnClickListener。你没有提到,你获取的 itemUri 不同的价值观,但我认为你需要该行的ID来获得它。

When the user clicks on the row it will still call onListItemClick(), except if they click on the comments box. The comments box fires the OnClickListener above where you can direct the user to your CommentsActivity. You didn't mention where you fetch the different values for itemUri but I assumed you need the row's id to get it.

在你的previous的问题,我注意到你正在做一些重复的调用和蒂亚戈·莫雷拉Rocha的布局非常复杂,无法重复使用(每一个的ListView行。)因此,我提出了一个不同的方法。我分了我的答案为部分有关适配器,排布局和颜色 comments_count

In your previous question, I noticed that you are making some repetitive calls and that Thiago Moreira Rocha's layout was extremely complex to be used repeatedly (for every ListView row.) So I propose a different approach. I've divided my answer into parts relating to the adapter, row layout, and colors for comments_count:

的适配器
我会后在code完全,然后在底部解释:

The Adapter
I'll post the code in full and then explain at the bottom:

public class CustomCursorAdapter extends CursorAdapter {
    private LayoutInflater mInflater;
    private int[] mFrom;

    private OnClickListener commentClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Get the position saved in bindView()
            long id = (Long) v.getTag();
            Toast.makeText(v.getContext(), "Comment Click: " + id, Toast.LENGTH_SHORT).show();
        }
    };

    public CustomCursorAdapter(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);
        mInflater = LayoutInflater.from(context);
    }

    private void applyColorFilter(Drawable drawable, int count) {
        drawable.clearColorFilter();
        if (count > 0) {
            float saturation = (count * 15) / 100f;
            // The value gets pinned if out of range.
            int color = Color.HSVToColor(new float[] {110f, saturation, 1f});
            drawable.setColorFilter(color, Mode.SRC);
        }
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.title.setText(cursor.getString(mFrom[0]));
        holder.description.setText(cursor.getString(mFrom[1]));

        // Get comments_count and set it as text
        int count = cursor.getInt(mFrom[2]);
        holder.comments_count.setText(count + "");
        holder.comments_count.setTag(cursor.getLong(0));

        // Adjust the color by saturation
        applyColorFilter(holder.comments_color, count);

        // Alternate method, that I explain in the answer
        //   Note: set the solid color in color.xml to #2aff00 
        //holder.comments_color.setAlpha(count * 45);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = mInflater.inflate(R.layout.list_item, parent, false);

        ViewHolder holder = new ViewHolder();
        holder.title = (TextView)view.findViewById(R.id.title);
        holder.description = (TextView)view.findViewById(R.id.description);
        holder.comments_count = (TextView)view.findViewById(R.id.comments_count);
        holder.comments_count.setOnClickListener(commentClick);
        holder.comments_color = ((LayerDrawable) holder.comments_count.getBackground()).findDrawableByLayerId(R.id.color);

        view.setTag(holder);

        return view;
    }

    @Override
    public Cursor swapCursor(Cursor newCursor) {
        if(mFrom == null && newCursor != null) {
            mFrom = new int[] {newCursor.getColumnIndex(TITLE), newCursor.getColumnIndex(DESCRIPTION), newCursor.getColumnIndex(COMMENTS_COUNT)};
        }
        return super.swapCursor(newCursor);
    }

    private class ViewHolder {
        TextView title;
        TextView description;
        TextView comments_count;
        Drawable comments_color;
    }
}

我做了一些改动:

I made a few changes:

  • mFrom 认为,您所使用的列的索引。你只需要得到列索引的一次的,它不会,除非你改变光标更改
  • commentsClick 一个的,我使用的通用OnClickListener的每次的行,我将它设置在创建一个<$ C C $> ViewHolder
  • 我带了你的方法改变HSV颜色插入适配器,并把它称为 applyColorFilter()
  • 在我搬到创建 ViewHolder NewView的()而不是检查了之一 bindView()
  • mFrom holds the indices of the columns that you are using. You only need to get the column index once, it won't change unless you change the Cursor
  • commentsClick is one generic OnClickListener that I use for every row and I set it while creating a ViewHolder
  • I brought your method for changing the HSV color into the adapter and called it applyColorFilter()
  • I moved creating the ViewHolder into newView() rather than checking for a null one in bindView()

行布局
你可能注意到,我改变意见颜色有点不同,那是因为我用一个简单的排布置:

The Row Layout
You probably noticed that I change the comments' color a little differently, that is because I use a simpler row layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/comments_count"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/title"
        android:layout_toLeftOf="@+id/comments_count"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/comments_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="15dp"
        android:background="@drawable/comments_layers"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

(尽管蒂亚戈·莫雷拉Rocha的布局工作,嵌套ViewGroups似乎有点小题大做。任何时候你都只有一个一个的ViewGroup孩子,他们通常是一个替代方法。)

(While Thiago Moreira Rocha's layout works, the nested ViewGroups seem like overkill. Anytime you have a ViewGroup with only one child, their is usually an alternative.)

我用LayerDrawable来代替两个LinearLayouts,我将解释在步骤。 首先,的边界(<$ C C $> border.xml ),非常类似于previous之一:

I use a LayerDrawable to replace the two LinearLayouts, that I will explain in in steps. First, the border (border.xml), very similar to the previous one:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="10dp" />
    <padding android:bottom="2dp" android:left="2dp" android:right="2dp" android:top="2dp" />
    <solid android:color="#ffffff" />
    <stroke android:width="2dp"
        android:color="#000000" />
</shape>

(注意填充是笔划的宽度。)

(Notice the padding is the width of the stroke.)

二,可调背景颜色( color.xml ):

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <corners android:radius="10dp" />
    <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
    <solid android:color="#ffffff" />
</shape>

最后,我创建了一个LayerDrawable两个图像结合起来(<$ C C $> comments_layers.xml ):

Last, I created a LayerDrawable to combine the two images (comments_layers.xml):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item 
        android:id="@+id/border"
        android:drawable="@drawable/border" />
    <item 
        android:id="@+id/color"
        android:drawable="@drawable/color" />
</layer-list>


(可选)
您调整HSV值的 applyColorFilter()饱和,但似乎这是相当于调整的绿色背景的Alpha。如果这是真的,不断变化的alpha值是一个简单的任务。查找我的意见 bindView()


(Optional)
You adjust the saturation of your HSV value in applyColorFilter(), but it seems that this is the equivalent to adjusting the alpha of a green background. If this is true, the changing the alpha value is a much simpler task. Find my comments in bindView():

  1. 注释掉 applyColorFilter(holder.comments_color,计数);
  2. 取消注释 holder.comments_color.setAlpha(数* 45);
  3. 打开我的 color.xml 文件并修改颜色固体属性 #FFFFFF 到#2aff00
  1. Comment out applyColorFilter(holder.comments_color, count);
  2. Uncomment holder.comments_color.setAlpha(count * 45);
  3. Open my color.xml file and change the color attribute of the solid element from #ffffff to #2aff00

在所有实话我从未使用过LayerDrawables这样之前,有可能是一个更快的方式,但我认为这是pretty的光滑。


In all truth I had never used LayerDrawables like this before, there may be a faster way, but I think this is pretty slick.

这篇关于如何将多个触摸动作一个单独的列表项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆