[英]onItemClickListener: How to make two different listeners on each view?
I've got an item of list view. 我有一个列表视图项。 It has textview and imageview.
它具有textview和imageview。 After clicking on imageview it has to create intent after clicking on text view it has to strike text in it.
单击imageview后,它必须在单击文本视图后创建意图,它必须在其中击中文本。 I have created intent.
我创造了意图。 Created the list.
创建列表。 But dont know how to separate listener for two views.
但是不知道如何将两个视图的监听器分开。
Thats my code xml 那就是我的代码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);
}
Try this. 尝试这个。 It may help you.
它可能会帮助您。
// 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) {
}
}
});
In adapter getView() method you can set OnClickListeners separately for both textView and imageView. 在适配器的getView()方法中,可以分别为textView和imageView设置OnClickListeners。
For example like this 像这样
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
}
});
you can done this by define imageview and textview like this first give ids 您可以通过定义imageview和textview来做到这一点,首先给id
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
}
});
i hope this is helpfull 我希望这会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.