gpt4 book ai didi

java - onItemClickListener : How to make two different listeners on each view?

转载 作者:太空宇宙 更新时间:2023-11-04 11:18:08 25 4
gpt4 key购买 nike

我有一个 ListView 项目。它有 TextView 和 ImageView 。单击 ImageView 后,它必须创建 Intent ,单击 TextView 后,它必须敲击其中的文本。我已经创造了 Intent 。创建了列表。但不知道如何区分两个 View 的监听器。

这是我的代码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);
}

最佳答案

试试这个。它可能对你有帮助。

// 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) {

}
}
});

关于java - onItemClickListener : How to make two different listeners on each view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45229011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com