一个 ArrayList 项目的多个意图
Multiple Intents on one ArrayList item
我有一个包含几家酒店详细信息的 ArrayList,我想做的是点击地址打开地图意图,然后点击 phone 数字打开 phone.
如有任何帮助,我们将不胜感激。
这些是代码片段。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.item_list, container, false);
final ArrayList<pojo> pojoArrayList = new ArrayList<pojo>();
pojoArrayList.add(new pojo(getString(R.string.hotel_one), R.drawable.hotel_one, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_one_location), R.drawable.ic_phone_solid, getString(R.string.hotel_one_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_two), R.drawable.hotel_tow, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_two_location), R.drawable.ic_phone_solid,getString(R.string.hotel_two_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_three), R.drawable.hotel_three, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_three_location), R.drawable.ic_phone_solid,getString(R.string.hotel_three_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_four), R.drawable.hotel_four, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_four_location), R.drawable.ic_phone_solid,getString(R.string.hotel_four_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_five), R.drawable.hotel_five, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_five_location), R.drawable.ic_phone_solid,getString(R.string.hotel_five_phone)));
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList);
ListView listViewItems = (ListView) rootView.findViewById(R.id.list);
listViewItems.setAdapter(adapter);
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
if (position == 0) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31202246280"));
startActivity(callIntent);
} else if (position == 1) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 506 3715"));
startActivity(callIntent);
} else if (position == 2){
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 881 2595"));
startActivity(callIntent);
} else if (position == 3) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 665 1171"));
startActivity(callIntent);
} else {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 210 3535"));
startActivity(callIntent);
}
}
});
return rootView;
}
这是它的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hotel_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="@+id/category_image"
android:layout_width="match_parent"
android:layout_height="110dp"
android:scaleType="centerCrop"
android:src="@drawable/hotel" />
<View
android:id="@+id/grad"
android:layout_height="200dp"
android:layout_width="match_parent"
android:background="@drawable/gradient_splash"
android:alpha="0.4" />
<LinearLayout
android:layout_height="200dp"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/category_header"
android:layout_width="match_parent"
android:layout_height="110dp"
android:text="Hotels"
android:gravity="center_vertical"
android:textColor="@color/textBlack"
android:textSize="25sp"
android:textAlignment="center" />
<LinearLayout
android:id="@+id/location_layout"
android:layout_height="45dp"
android:layout_width="match_parent"
android:paddingLeft="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/location_icon"
android:layout_height="20dp"
android:layout_width="20dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/ic_map_" />
<TextView
android:id="@+id/location_data"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hotel_one_location"/>
</LinearLayout>
<LinearLayout
android:id="@+id/phone_layout"
android:layout_height="45dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingLeft="5dp">
<ImageView
android:id="@+id/phone_icon"
android:layout_height="20dp"
android:layout_width="20dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/ic_phone_solid" />
<TextView
android:id="@+id/phone_data"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hotel_one_phone"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
适配器:
public class HotelsAdapter extends ArrayAdapter<pojo>{
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listView = convertView;
if (listView == null){
listView = LayoutInflater.from(getContext()).inflate(R.layout.hotel_items, parent, false);
}
pojo currentItem = getItem(position);
ImageView backgroundImage = (ImageView) listView.findViewById(R.id.category_image);
backgroundImage.setImageResource(currentItem.getImageResource());
TextView categoryHeader = (TextView) listView.findViewById(R.id.category_header);
categoryHeader.setText(currentItem.getHeaderResource());
View categoryGradient = (View) listView.findViewById(R.id.grad);
categoryGradient.setBackgroundResource(currentItem.getGradientResource());
ImageView locationIcon = (ImageView) listView.findViewById(R.id.location_icon);
locationIcon.setImageResource(currentItem.getLocationIconID());
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
ImageView phoneIcon = (ImageView) listView.findViewById(R.id.phone_icon);
phoneIcon.setImageResource(currentItem.getPhoneIconID());
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
return listView;
}
}
到目前为止,我只有一个打开拨号器的意图。我如何 select location 部分和 phone 部分分开,并且对两者有不同的意图?
试试这个
First i suggest to use Recylerview because Recylerview is the advanced one.
在此处检查:https://developer.android.com/reference/android/support/v7/widget/RecyclerView
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
LinearLayout linear_phone = view.findViewById(R.id.phone_layout);
linear_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse(pojo.getPhoneID()));
startActivity(callIntent);
}
});
LinearLayout linear_location = view.findViewById(R.id.location_layout);
linear_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Location Intent Code
}
});
}
});
试试这个,
public class HotelsAdapter extends ArrayAdapter<pojo> {
private Context mContext;
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
this.mContext = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listView = convertView;
if (listView == null) {
listView = LayoutInflater.from(getContext()).inflate(R.layout.hotel_items, parent, false);
}
final pojo currentItem = getItem(position);
ImageView backgroundImage = (ImageView) listView.findViewById(R.id.category_image);
backgroundImage.setImageResource(currentItem.getImageResource());
TextView categoryHeader = (TextView) listView.findViewById(R.id.category_header);
categoryHeader.setText(currentItem.getHeaderResource());
View categoryGradient = (View) listView.findViewById(R.id.grad);
categoryGradient.setBackgroundResource(currentItem.getGradientResource());
ImageView locationIcon = (ImageView) listView.findViewById(R.id.location_icon);
locationIcon.setImageResource(currentItem.getLocationIconID());
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
ImageView phoneIcon = (ImageView) listView.findViewById(R.id.phone_icon);
phoneIcon.setImageResource(currentItem.getPhoneIconID());
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
// add this code in your adapter
LinearLayout locationLayout = (LinearLayout) listView.findViewById(R.id.location_layout)
locationLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent geoIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri
.parse("geo:0,0?q=" + currentItem.getAddressID()));
mContext.startActivity(geoIntent);
}
});
LinearLayout phoneLayout = (LinearLayout) listView.findViewById(R.id.phone_layout)
locationLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: " + currentItem.getPhoneID()));
mContext.startActivity(callIntent);
}
});
return listView;
}
}
并删除 listview.onitemClicklistener();
如果您不希望整个列表项都可以点击,而是希望列表中有可点击的元素,您应该实现自定义侦听器
创建一个侦听器接口,为每个应该可点击的单独子视图指定一个方法,在您的 Activity/Fragment 中实现它,然后将它传递给您的自定义列表适配器。
您的自定义适配器随后将根据单击的字段调用侦听器的方法。
示例界面
public interface HotelListClickListener {
void onAddressClicked(Pojo item);
void onPhoneClicked(Pojo item);
...
}
然后您应该修改您的 HotelsAdapter 以在某个点接收侦听器(构造函数,或 Setter),然后在您的 getView 方法中调用每个适当的方法。
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
...
Pojo currentItem = getItem(position);
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
addressView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener != null)
listener.onAddressClicked(currentItem);
}
});
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
phoneView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener != null)
listener.onPhoneClicked(currentItem);
}
});
...
}
最后,在您的 Activity/Fragment 中,您将创建一个全局侦听器,该侦听器将在单击特定子视图时接收适当的 POJO,然后处理被单击的任何元素。
HotelListClickListener listener = new HotelListClickListener() {
@Override
public void onAddressClicked(Pojo item) {
// Show map view based on item's address
}
@Override
public void onPhoneClicked(Pojo item) {
// Call number of item that was clicked
}
};
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList, listener);
为 phoneView 和 addressView 添加 View.Onclicklistener
public class HotelsAdapter extends ArrayAdapter<pojo>{
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
/****your code*****/
//add listener
addressView.setOnClickListener(mClickListener);
phoneView.setOnClickListener(mClickListener);
return listView;
}
//add View.Onclicklistener for phoneView and addressView
private OnClickListener mClickListener;
public void setOptionClickListener(OnClickListener listener) {
this.mClickListener = listener;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.item_list, container, false);
final ArrayList<pojo> pojoArrayList = new ArrayList<pojo>();
pojoArrayList.add(new pojo(getString(R.string.hotel_one), R.drawable.hotel_one, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_one_location), R.drawable.ic_phone_solid, getString(R.string.hotel_one_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_two), R.drawable.hotel_tow, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_two_location), R.drawable.ic_phone_solid,getString(R.string.hotel_two_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_three), R.drawable.hotel_three, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_three_location), R.drawable.ic_phone_solid,getString(R.string.hotel_three_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_four), R.drawable.hotel_four, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_four_location), R.drawable.ic_phone_solid,getString(R.string.hotel_four_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_five), R.drawable.hotel_five, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_five_location), R.drawable.ic_phone_solid,getString(R.string.hotel_five_phone)));
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList);
ListView listViewItems = (ListView) rootView.findViewById(R.id.list);
listViewItems.setAdapter(adapter);
//when you click list's item
pojo currentTarget;
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
currentTarget = pojo
if (position == 0) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31202246280"));
startActivity(callIntent);
} else if (position == 1) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 506 3715"));
startActivity(callIntent);
} else if (position == 2){
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 881 2595"));
startActivity(callIntent);
} else if (position == 3) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 665 1171"));
startActivity(callIntent);
} else {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 210 3535"));
startActivity(callIntent);
}
}
});
adapter.setOptionClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.location_data:
//TODO: currentTarget's data that opens up a map
break;
case R.id.phone_data:
//TODO: currentTarget's data that opens up the phone
break;
}
}
});
return rootView;
}
我有一个包含几家酒店详细信息的 ArrayList,我想做的是点击地址打开地图意图,然后点击 phone 数字打开 phone.
如有任何帮助,我们将不胜感激。 这些是代码片段。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.item_list, container, false);
final ArrayList<pojo> pojoArrayList = new ArrayList<pojo>();
pojoArrayList.add(new pojo(getString(R.string.hotel_one), R.drawable.hotel_one, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_one_location), R.drawable.ic_phone_solid, getString(R.string.hotel_one_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_two), R.drawable.hotel_tow, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_two_location), R.drawable.ic_phone_solid,getString(R.string.hotel_two_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_three), R.drawable.hotel_three, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_three_location), R.drawable.ic_phone_solid,getString(R.string.hotel_three_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_four), R.drawable.hotel_four, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_four_location), R.drawable.ic_phone_solid,getString(R.string.hotel_four_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_five), R.drawable.hotel_five, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_five_location), R.drawable.ic_phone_solid,getString(R.string.hotel_five_phone)));
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList);
ListView listViewItems = (ListView) rootView.findViewById(R.id.list);
listViewItems.setAdapter(adapter);
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
if (position == 0) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31202246280"));
startActivity(callIntent);
} else if (position == 1) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 506 3715"));
startActivity(callIntent);
} else if (position == 2){
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 881 2595"));
startActivity(callIntent);
} else if (position == 3) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 665 1171"));
startActivity(callIntent);
} else {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 210 3535"));
startActivity(callIntent);
}
}
});
return rootView;
}
这是它的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hotel_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ImageView
android:id="@+id/category_image"
android:layout_width="match_parent"
android:layout_height="110dp"
android:scaleType="centerCrop"
android:src="@drawable/hotel" />
<View
android:id="@+id/grad"
android:layout_height="200dp"
android:layout_width="match_parent"
android:background="@drawable/gradient_splash"
android:alpha="0.4" />
<LinearLayout
android:layout_height="200dp"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/category_header"
android:layout_width="match_parent"
android:layout_height="110dp"
android:text="Hotels"
android:gravity="center_vertical"
android:textColor="@color/textBlack"
android:textSize="25sp"
android:textAlignment="center" />
<LinearLayout
android:id="@+id/location_layout"
android:layout_height="45dp"
android:layout_width="match_parent"
android:paddingLeft="5dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/location_icon"
android:layout_height="20dp"
android:layout_width="20dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/ic_map_" />
<TextView
android:id="@+id/location_data"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hotel_one_location"/>
</LinearLayout>
<LinearLayout
android:id="@+id/phone_layout"
android:layout_height="45dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingLeft="5dp">
<ImageView
android:id="@+id/phone_icon"
android:layout_height="20dp"
android:layout_width="20dp"
android:layout_margin="5dp"
android:layout_gravity="center"
android:src="@drawable/ic_phone_solid" />
<TextView
android:id="@+id/phone_data"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/hotel_one_phone"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
适配器:
public class HotelsAdapter extends ArrayAdapter<pojo>{
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listView = convertView;
if (listView == null){
listView = LayoutInflater.from(getContext()).inflate(R.layout.hotel_items, parent, false);
}
pojo currentItem = getItem(position);
ImageView backgroundImage = (ImageView) listView.findViewById(R.id.category_image);
backgroundImage.setImageResource(currentItem.getImageResource());
TextView categoryHeader = (TextView) listView.findViewById(R.id.category_header);
categoryHeader.setText(currentItem.getHeaderResource());
View categoryGradient = (View) listView.findViewById(R.id.grad);
categoryGradient.setBackgroundResource(currentItem.getGradientResource());
ImageView locationIcon = (ImageView) listView.findViewById(R.id.location_icon);
locationIcon.setImageResource(currentItem.getLocationIconID());
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
ImageView phoneIcon = (ImageView) listView.findViewById(R.id.phone_icon);
phoneIcon.setImageResource(currentItem.getPhoneIconID());
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
return listView;
}
}
到目前为止,我只有一个打开拨号器的意图。我如何 select location 部分和 phone 部分分开,并且对两者有不同的意图?
试试这个
First i suggest to use Recylerview because Recylerview is the advanced one.
在此处检查:https://developer.android.com/reference/android/support/v7/widget/RecyclerView
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
LinearLayout linear_phone = view.findViewById(R.id.phone_layout);
linear_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse(pojo.getPhoneID()));
startActivity(callIntent);
}
});
LinearLayout linear_location = view.findViewById(R.id.location_layout);
linear_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Location Intent Code
}
});
}
});
试试这个,
public class HotelsAdapter extends ArrayAdapter<pojo> {
private Context mContext;
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
this.mContext = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listView = convertView;
if (listView == null) {
listView = LayoutInflater.from(getContext()).inflate(R.layout.hotel_items, parent, false);
}
final pojo currentItem = getItem(position);
ImageView backgroundImage = (ImageView) listView.findViewById(R.id.category_image);
backgroundImage.setImageResource(currentItem.getImageResource());
TextView categoryHeader = (TextView) listView.findViewById(R.id.category_header);
categoryHeader.setText(currentItem.getHeaderResource());
View categoryGradient = (View) listView.findViewById(R.id.grad);
categoryGradient.setBackgroundResource(currentItem.getGradientResource());
ImageView locationIcon = (ImageView) listView.findViewById(R.id.location_icon);
locationIcon.setImageResource(currentItem.getLocationIconID());
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
ImageView phoneIcon = (ImageView) listView.findViewById(R.id.phone_icon);
phoneIcon.setImageResource(currentItem.getPhoneIconID());
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
// add this code in your adapter
LinearLayout locationLayout = (LinearLayout) listView.findViewById(R.id.location_layout)
locationLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent geoIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri
.parse("geo:0,0?q=" + currentItem.getAddressID()));
mContext.startActivity(geoIntent);
}
});
LinearLayout phoneLayout = (LinearLayout) listView.findViewById(R.id.phone_layout)
locationLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: " + currentItem.getPhoneID()));
mContext.startActivity(callIntent);
}
});
return listView;
}
}
并删除 listview.onitemClicklistener();
如果您不希望整个列表项都可以点击,而是希望列表中有可点击的元素,您应该实现自定义侦听器
创建一个侦听器接口,为每个应该可点击的单独子视图指定一个方法,在您的 Activity/Fragment 中实现它,然后将它传递给您的自定义列表适配器。
您的自定义适配器随后将根据单击的字段调用侦听器的方法。
示例界面
public interface HotelListClickListener {
void onAddressClicked(Pojo item);
void onPhoneClicked(Pojo item);
...
}
然后您应该修改您的 HotelsAdapter 以在某个点接收侦听器(构造函数,或 Setter),然后在您的 getView 方法中调用每个适当的方法。
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
...
Pojo currentItem = getItem(position);
TextView addressView = (TextView) listView.findViewById(R.id.location_data);
addressView.setText(currentItem.getAddressID());
addressView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener != null)
listener.onAddressClicked(currentItem);
}
});
TextView phoneView = (TextView) listView.findViewById(R.id.phone_data);
phoneView.setText(currentItem.getPhoneID());
phoneView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(listener != null)
listener.onPhoneClicked(currentItem);
}
});
...
}
最后,在您的 Activity/Fragment 中,您将创建一个全局侦听器,该侦听器将在单击特定子视图时接收适当的 POJO,然后处理被单击的任何元素。
HotelListClickListener listener = new HotelListClickListener() {
@Override
public void onAddressClicked(Pojo item) {
// Show map view based on item's address
}
@Override
public void onPhoneClicked(Pojo item) {
// Call number of item that was clicked
}
};
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList, listener);
为 phoneView 和 addressView 添加 View.Onclicklistener
public class HotelsAdapter extends ArrayAdapter<pojo>{
public HotelsAdapter(@NonNull Context context, ArrayList<pojo> pojo) {
super(context, 0, pojo);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
/****your code*****/
//add listener
addressView.setOnClickListener(mClickListener);
phoneView.setOnClickListener(mClickListener);
return listView;
}
//add View.Onclicklistener for phoneView and addressView
private OnClickListener mClickListener;
public void setOptionClickListener(OnClickListener listener) {
this.mClickListener = listener;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.item_list, container, false);
final ArrayList<pojo> pojoArrayList = new ArrayList<pojo>();
pojoArrayList.add(new pojo(getString(R.string.hotel_one), R.drawable.hotel_one, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_one_location), R.drawable.ic_phone_solid, getString(R.string.hotel_one_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_two), R.drawable.hotel_tow, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_two_location), R.drawable.ic_phone_solid,getString(R.string.hotel_two_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_three), R.drawable.hotel_three, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_three_location), R.drawable.ic_phone_solid,getString(R.string.hotel_three_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_four), R.drawable.hotel_four, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_four_location), R.drawable.ic_phone_solid,getString(R.string.hotel_four_phone)));
pojoArrayList.add(new pojo(getString(R.string.hotel_five), R.drawable.hotel_five, R.drawable.gradient_splash, R.drawable.ic_map_, getString(R.string.hotel_five_location), R.drawable.ic_phone_solid,getString(R.string.hotel_five_phone)));
HotelsAdapter adapter = new HotelsAdapter(getActivity(), pojoArrayList);
ListView listViewItems = (ListView) rootView.findViewById(R.id.list);
listViewItems.setAdapter(adapter);
//when you click list's item
pojo currentTarget;
listViewItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
pojo pojo = pojoArrayList.get(position);
currentTarget = pojo
if (position == 0) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31202246280"));
startActivity(callIntent);
} else if (position == 1) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 506 3715"));
startActivity(callIntent);
} else if (position == 2){
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 881 2595"));
startActivity(callIntent);
} else if (position == 3) {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 665 1171"));
startActivity(callIntent);
} else {
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel: +31 20 210 3535"));
startActivity(callIntent);
}
}
});
adapter.setOptionClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.location_data:
//TODO: currentTarget's data that opens up a map
break;
case R.id.phone_data:
//TODO: currentTarget's data that opens up the phone
break;
}
}
});
return rootView;
}