我想创建一个我有ListView
的应用。
我希望我的ListView
看起来像下图。但问题是只有列表中的Button
可以点击,没有别的。我怎样才能实现它
CustomList.Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff">
<LinearLayout
android:orientation="vertical"
android:layout_width="250dp"
android:background="#fff"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Job Code"
android:id="@+id/tv_job_code"
style="@style/Job_on_Call"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category"
android:id="@+id/tv_category"
style="@style/Job_on_Call"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="("
android:id="@+id/textView4"
style="@style/Job_on_Call"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Years"
android:id="@+id/tv_exp_yrs"
style="@style/Job_on_Call"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=","
android:id="@+id/textView5"
style="@style/Job_on_Call"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mnths"
android:id="@+id/tv_exp_mnths"
style="@style/Job_on_Call"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")"
android:id="@+id/textView45"
style="@style/Job_on_Call"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Posted On:"
style="@style/Job_on_Call"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6-12-2014"
android:id="@+id/tv_date"
style="@style/Job_on_Call"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Matching Jobs"
android:id="@+id/button"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Applied Jobs"
android:id="@+id/button2"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
CustomAdapter
package com.example.jobs_on_call_adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.jobs_on_call.app.R;
import com.example.jobs_on_call_object.SearchValues;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by ANUJ on 6/11/2014.
*/
public class SearchJobsCustomList extends BaseAdapter {
Context c;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String> ();
ArrayList<SearchValues> values;
SearchValues a;
public SearchJobsCustomList(Context c, ArrayList<SearchValues> values) {
super ();
this.c = c;
this.values = values;
}
@Override
public int getCount() {
return values.size ();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
a = new SearchValues ();
if (view == null) {
view = LayoutInflater.from (c).inflate (R.layout.custom_search_jobs_lists, viewGroup, false);
a = values.get (i);
TextView JobCode = (TextView) view.findViewById (R.id.tv_job_code);
TextView Category = (TextView) view.findViewById (R.id.tv_category);
TextView ExpYrs = (TextView) view.findViewById (R.id.tv_exp_yrs);
TextView ExpMnths = (TextView) view.findViewById (R.id.tv_exp_mnths);
TextView Date = (TextView) view.findViewById (R.id.tv_date);
JobCode.setText (a.code);
Category.setText (a.title);
// ExpYrs.setText (resultp.get ("minExp"));
// ExpMnths.setText (resultp.get ("maxExp"));
// Date.setText (resultp.get ("postedOn"));
// JobCode.setText ("CGT011");
// Category.setText ("Press");
// ExpYrs.setText ("1 Year");
// ExpMnths.setText ("5 Months");
// Date.setText ("6-6-14");
}
return view;
}
}
答案 0 :(得分:2)
ListView listview = (ListView)findViewById(R.id.list);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//Do your code here
}
});
答案 1 :(得分:1)
private class CustomList extends BaseAdapter
{
LayoutInflater inflater;
public CustomList()
{
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return arrItem.size();
}
@Override
public Object getItem(int position)
{
return null;
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parentView)
{
ViewHolder holder;
if (convertView == null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.Yourlayout, parentView, false);
holder.btn1=(RelativeLayout)convertView.findViewById(R.id.btn1);
holder.btn2=(RelativeLayout)convertView.findViewById(R.id.btn2);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.btn1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//apply your logic here.
// you can get data here like. items.get(position);
//items is custom Arraylist , ArrayList<Items> items=new ArrayList<Items>();
}
});
holder.btn2.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//apply your logic here.
}
});
convertView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//apply your logic here.
}
});
return convertView;
}
class ViewHolder
{
Button btn1,btn2;
}
}
一种简单的方法...... nJoy:)
答案 2 :(得分:0)
ListView lv = (ListView)findViewById(R.id.list);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//here your logic
}
});
答案 3 :(得分:0)
您可以尝试在其onclicklistener
中更改自定义列表视图项目的背景yourlistview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
holder.yourlayout.setBackgroundColor(Color.CYAN);
}
});
答案 4 :(得分:0)
在CustomList.Xml中的按钮属性中使用android:focusable="false"
。
这应该可以解决问题。
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Matching Jobs"
android:focusable="false"
android:id="@+id/button"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Applied Jobs"
android:focusable="false"
android:id="@+id/button2"/>
</LinearLayout>
</LinearLayout>
别忘了在列表视图中添加setOnItemClickListener。
答案 5 :(得分:0)
您需要使用setOnItemClickListener()
来检测ListView项目的点击次数。
然而,如果您将可聚焦视图放在ListView
上,则ListView的OnItemClickListener将不会触发。因此,您还需要将android:focusable
和android:focusableInTouchMode
设置为ListView项目布局中的Button
小部件。