gpt4 book ai didi

java - setOnClickListener 未使用自定义适配器和自定义 ListView 触发

转载 作者:太空宇宙 更新时间:2023-11-04 13:49:51 26 4
gpt4 key购买 nike

我花了几个小时解决一个问题,但仍然无法解决。当我单击 ListView 中的项目时,HotelOverviewFragment 中的 setOnClickListener 不会触发。但是,setOnClickListener 确实可以在我的自定义适配器 (NowArrayAdapter) 中工作。

我的问题是:为什么 setOnClickListener 在 HotelOverviewFragment(显示 ListView 的类)中不起作用?

这是我尝试过的列表:

  • 在 hotel_row_layout.xml 和fragment_hotel_overview.xml 中设置 android:focusable="false"android:focusableInTouchMode="false"android:descendantFocusability="blocksDescendants"

  • android:clickable 设置为 true 和 false。两者都不起作用。

  • 从 BaseAdapter 实现更改为 arrayAdapter。

  • 我在 HotelOverviewFragment 中尝试了不同的监听器:setOnClickListenersetOnItemClickListenersetOnItemSelectedListenersetOnTouchListener。不幸的是,这些都不适合我。

这是我的代码:

自定义适配器

    public class NowArrayAdapter extends ArrayAdapter<String> {

private Context context;
private ArrayList<String> values;
private Typeface typeface;
private static Hashtable fontCache = new Hashtable();
private LayoutInflater inflater;
private TextView item;

public NowArrayAdapter(Context context, ArrayList<String> commandsList) {
super(context, R.layout.hotel_row_layout, commandsList);
this.context = context;
values = new ArrayList<String>();
values.addAll(commandsList);

typeface = getTypeface(this.context, "fonts/Roboto-Light.ttf");
inflater = LayoutInflater.from(this.context);
}

static Typeface getTypeface(Context context, String font) {
Typeface typeface = (Typeface)fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}

public View getView(int position, View convertView, ViewGroup parent) {

String myText = getItem(position);

if(convertView == null) {
convertView = inflater.inflate(R.layout.hotel_row_layout, parent, false);

item = (TextView) convertView.findViewById(R.id.maps_button);
item.setTypeface(typeface);

convertView.setTag(item);
} else {
item = (TextView) convertView.getTag();
}

item.setText(myText);
//myListItem.descText.setTextSize(14);

convertView.setOnClickListener(new View.OnClickListener() {
// WORKS!
@Override
public void onClick(View view) {
Log.d("click", "Don't look at me!");
}
});

return convertView;
}

}

fragment

    public class HotelOverviewFragment extends Fragment {
private static Hashtable fontCache = new Hashtable();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_hotel_overview, container, false);
ListView list = (ListView) v.findViewById(android.R.id.list);

// Set up listview and buttons
setUp(v, list);

// Inflate the layout for this fragment
return v;
}

static Typeface getTypeface(Context context, String font) {
Typeface typeface = (Typeface)fontCache.get(font);
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), font);
fontCache.put(font, typeface);
}
return typeface;
}

public void setUp(View v, ListView l){
TextView address = (TextView) v.findViewById(R.id.content);
TextView header = (TextView) v.findViewById(R.id.header);
TextView hotelName = (TextView) v.findViewById(R.id.hotelName);
Typeface typeface = getTypeface(getActivity(), "fonts/Roboto-Light.ttf");

address.setText("some street \nZipCode, City \nCountry \nEmail \nphoneNumber");
address.setTypeface(typeface);

header.setText("Hotel info");
header.setTypeface(typeface);
header.setTextSize(20);

hotelName.setText("Hotel name");
hotelName.setTypeface(typeface);

// Set up button
ArrayList<String> n = new ArrayList<String>();
n.add(0, "More info");

// Show button
NowArrayAdapter adapter = new NowArrayAdapter(getActivity(), n);

l.setAdapter(adapter);

// THIS IS THE STUFF I'VE BEEN TRYING
try {
l.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("click", "Success");
}
});

l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("click", "Success");
}
});

l.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d("click", "Success");
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});

l.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("click", "Success");

return false;
}
});
}catch (Exception e){
Log.d("click", e + "");
}
}


}

HotelOverviewFragment 的布局 xml

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example"
android:background="#ffebebeb">

<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:src="@drawable/banner"
android:id="@+id/hotelBanner"
android:layout_gravity="top"
android:adjustViewBounds="false"
android:scaleType="fitXY" />

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hotelname"
android:id="@+id/hotelName"
android:gravity="center"
android:textSize="40dp"
android:layout_alignBottom="@+id/hotelBanner"
android:layout_alignParentRight="false"
android:layout_alignParentLeft="false"
android:textColor="#ffc4c4c4"
android:layout_marginBottom="5dp" />
</RelativeLayout>
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="0dp"
android:background="@drawable/header_card">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/header"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/primary_text_light"
android:layout_height="wrap_content"
android:text="Header"
android:layout_toRightOf="@+id/headerImage"
android:layout_centerVertical="true" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/headerImage"
android:src="@drawable/ic_action_live_help"
android:layout_centerVertical="true" />
</RelativeLayout>

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:background="@drawable/content_card">

<TextView
android:id="@+id/content"
android:layout_gravity="left|center_vertical"
android:layout_width="fill_parent"
android:layout_weight="1"
android:textColor="@android:color/primary_text_light"
android:layout_height="wrap_content"
android:text="Content"
/>
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">

<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"/>

</LinearLayout>

</LinearLayout>

</ScrollView>

ListView 项的自定义 xml

 <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:background="@drawable/content_card">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/maps_button"
android:layout_gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="@android:color/primary_text_light"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />

<ImageView
android:src="@drawable/ic_action_arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="false"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="false"
android:layout_centerVertical="true"
/>

</RelativeLayout>
</LinearLayout>

提前致谢。

最佳答案

感谢 Daniel Nugent 的建议,我让它工作起来了。删除 convertView.setOnClickListener() 是答案的一部分。我认为它阻止了 HotelOverviewFragment 中的其他监听器。

我的下一个错误是我在 ListView 上使用 setOnClickListener 进行测试。setOnClickListener 应该用于按钮而不是 ListView。因此,删除 setOnClickListener 后,所有其他监听器都开始工作。

感谢您的宝贵时间。

关于java - setOnClickListener 未使用自定义适配器和自定义 ListView 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30417583/

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