我有一个项目列表视图,每个项目都有一些图像视图和文本视图。我用Google搜索了许多解决方案,但没有一个能帮助我。我对android开发很新,我想我正在失去一些明显的东西。我也假设我可能尝试了太多的解决方案而且它们以某种方式相互阻挡。拜托,帮帮我吧。 这是我的代码:
public class MyActivity extends Activity {
private List<Lesson> lessons = new ArrayList<Lesson>();
private int loop_count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
FillLessonList();
FillListView();
}
private void FillListView()
{
ArrayAdapter<Lesson> adapter = new MyListAdapter();
ListView list = (ListView) findViewById(R.id.lessonsList);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name));
Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(), LessonActivity.class);
startActivity(intent);
}
});
}
private class MyListAdapter extends ArrayAdapter<Lesson>
{
MyListAdapter()
{
super(MyActivity.this, R.layout.lesson_item, lessons);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View lessonView = convertView;
if(lessonView == null)
{
lessonView = getLayoutInflater().inflate(R.layout.lesson_item, parent, false);
}
Lesson currentLesson = lessons.get(position);
//here I add all the needed info into the item.
//This part works fine and displays everything I need properly.
return lessonView;
}
每次点击都会记录下来的唯一内容是:
05-18 17:10:57.158 29101-29101/h2d.drawteam.howtodraw D/AbsListView: Touch down: touch mode = 0,mScrollY = 0,y = 122.7923,mFirstPosition = 0,mActivePointerId = 0,mDataChanged = false,adatper size = 15,this = android.widget.ListView{32614ed4 VFED.VC. .F...... 60,0-1020,1599 #7f0d0076 app:id/lessonsList}
05-18 17:10:57.258 29101-29101/h2d.drawteam.howtodraw D/AbsListView: CheckForTap:mFirstPosition = 0,mMotionPosition = 0,child = android.widget.RelativeLayout{3506a91f V.E..... ........ 0,0-960,225}
05-18 17:10:57.293 29101-29101/h2d.drawteam.howtodraw D/AbsListView: Touch up: touch mode = 1,mScrollY = 0,mLastY = -2147483648,mMotionPosition = 0,mFirstPosition = 0,mDataChanged = false,adatper size = 15,this = android.widget.ListView{32614ed4 VFED.VC. .F...... 60,0-1020,1599 #7f0d0076 app:id/lessonsList}
这是我的主要布局xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_clear"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/container_button_back"
android:paddingRight="25dp"
android:paddingLeft="25dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:weightSum="1">
<Button
android:layout_width="64dp"
android:layout_height="31dp"
android:id="@+id/button_back"
android:background="@drawable/back_button_fit"
android:maxHeight="50dp"
android:maxWidth="80dp"
android:adjustViewBounds="true"
android:longClickable="false"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:singleLine="false" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/separator_1" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/container_list"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="5dp">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lessonsList"
android:dividerHeight="0dp"
android:divider="@null"
android:descendantFocusability="blocksDescendants"/>
</LinearLayout>
这是我的列表项xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:clickable="false"
android:paddingLeft="0dp"
android:paddingRight="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lesson_icon"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/abc_btn_check_material"
android:minHeight="75dp"
android:minWidth="75dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/default_lesson_name"
android:id="@+id/lesson_text_lesson_name"
android:layout_marginTop="17dp"
android:layout_alignParentTop="true"
android:textSize="18sp"
android:maxLines="1"
android:layout_toLeftOf="@+id/lesson_rate"
android:layout_toRightOf="@+id/lesson_icon"
android:layout_toEndOf="@+id/lesson_icon"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/default_steps"
android:id="@+id/lesson_text_steps"
android:layout_below="@+id/lesson_text_lesson_name"
android:layout_toRightOf="@+id/lesson_icon"
android:layout_toEndOf="@+id/lesson_icon"
android:textSize="12sp"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
<ImageButton
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/lesson_rate"
android:src="@drawable/rate1"
android:baselineAlignBottom="false"
android:background="#00000000"
android:layout_above="@+id/lesson_text_steps"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:paddingRight="10dp"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="centerInside"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lesson_separator"
android:src="@drawable/separator"
android:background="#00000000"
android:adjustViewBounds="true"
android:layout_alignBottom="@+id/lesson_icon"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"/>
答案 0 :(得分:1)
试试这个
list.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name));
Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(), LessonActivity.class);
startActivity(intent);
}
});
或
list.setOnItemClickListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i("debug", "item click: " + view.findViewById(R.id.lesson_text_lesson_name));
Toast.makeText(getApplicationContext(), "Your toast message.",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(view.getContext(), LessonActivity.class);
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
替代方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View lessonView = convertView;
if(lessonView == null)
{
lessonView = getLayoutInflater().inflate(R.layout.lesson_item, parent, false);
}
Lesson currentLesson = lessons.get(position);
//here I add all the needed info into the item.
//This part works fine and displays everything I need properly.
lessonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(""testing123,"testing");
Toast.makeText(MyActivity.this, "Your toast message.",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MyActivity.this, LessonActivity.class);
startActivity(intent);
}
});
return lessonView;
}
答案 1 :(得分:1)
由于您有自定义适配器,因此需要在getView()中包含onClickListener。因此,如果您想在用户点击时查看ImageView- lesson_icon时采取某些操作,那么您可能需要编写如下内容:
lessonView.findViewById(R.id.lesson_icon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Image clicked", Toast.LENGTH_SHORT).show();
}
});