onItemClickListener:如何在每个视图上创建两个不同的侦听器?

时间:2017-07-21 04:30:58

标签: java android android-layout

我有一个列表视图项目。它有textview和imageview。单击imageview后,它必须在单击文本视图后创建意图,它必须在其中标记文本。我创造了意图。创建了列表。但是不知道如何将侦听器分成两个视图。

这就是我的代码 XML

<?xml version="1.0" encoding="utf-8"?><!-- Layout for a single list item in the list of list -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/activity_margin"
        android:layout_weight="1"
        android:background="@color/colorAccent"
        >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-medium"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#2B3D4D"
            />

        <TextView
            android:id="@+id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:textAppearance="?android:textAppearanceSmall"
            android:textColor="#AEB6BD" />

    </LinearLayout>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_mode_edit_black_24dp"
        android:layout_weight="1"
        android:id="@+id/btn_edit"/>
</LinearLayout>

的java

public class CatalogActivity extends AppCompatActivity implements
        LoaderManager.LoaderCallbacks<Cursor> {

    /** Identifier for the pet data loader */
    private static final int LIST_LOADER = 0;

    /** Adapter for the ListView */
    ListCursorAdapter mCursorAdapter;

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

        // Setup FAB to open EditorActivity
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
                startActivity(intent);
            }
        });

        // Find the ListView which will be populated with the list data
        ListView listListView = (ListView) findViewById(R.id.list);

        // Find and set empty view on the ListView, so that it only shows when the list has 0 items.
        View emptyView = findViewById(R.id.empty_view);
        listListView.setEmptyView(emptyView);

        // Setup an Adapter to create a list item for each row of list data in the Cursor.
        // There is no items data yet (until the loader finishes) so pass in null for the Cursor.
        mCursorAdapter = new ListCursorAdapter(this, null);
        listListView.setAdapter(mCursorAdapter);

        // Setup the item click listener
        listListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                // Create new intent to go to {@link EditorActivity}
                Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);

                // Form the content URI that represents the specific item that was clicked on,
                // by appending the "id" (passed as input to this method) onto the
                // {@link ListEntry#CONTENT_URI}.
                // For example, the URI would be "content://com.example.android.list/list/2"
                // if the pet with ID 2 was clicked on.
                Uri currentItemUri = ContentUris.withAppendedId(ListContract.ListEntry.CONTENT_URI, id);

                // Set the URI on the data field of the intent
                intent.setData(currentItemUri);

                // Launch the {@link EditorActivity} to display the data for the current pet.
                startActivity(intent);
            }
        });

        // Kick off the loader
        getSupportLoaderManager().initLoader(LIST_LOADER, null, this);
    }

3 个答案:

答案 0 :(得分:0)

试试这个。它可能会帮助你。

// Setup the item click listener
listListView.setOnItemClickListener(new 
                             AdapterView.OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> adapterView, View 
                           view, int position, long id) {

       View selectedView = adapterView.getSelectedView();
        if (selectedView instanceof ImageView) {

        } else if (selectedView instanceof TextView) {

        }
    }
});

答案 1 :(得分:0)

在适配器getView()方法中,您可以为textView和imageView分别设置OnClickListeners。

例如像这样

imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do stuff for imageview
        }
    });

textView..setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do stuff for textview
        }
    });

答案 2 :(得分:0)

你可以通过定义imageview和textview来完成这个,就像第一次给出ids

一样
final ImageView iv=(ImageView)emptyView.findViewById(R.id.btn_edit);
//emptyview is your list item layout 
//similar for textview and you can now impement listner in onitemclicklistner

 listListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

//same for textview
}
});

我希望这是有帮助的