关于android BaseAdapter 中如何灵活控制listview 是否显示,是否有事件触发

如何使用baseAdapter

需求说明:

主要实现listview的灵活性。在一种情况下,item需要显示全部的布局,并且可以有点击事件,在另一种情况下,item不可以显示进入子菜单的箭头图片,也不可以有点击事件。

Xml布局文件:

1)  main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".task_activity" 
    android:background="@android:color/white"
    >
    <LinearLayout
        android:id="@+id/layout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"   
        />
</RelativeLayout>

2)  item.xml


<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout   
         xmlns:android="http://schemas.android.com/apk/res/android"   
         android:layout_height="wrap_content"    
         android:layout_width="match_parent"
         android:background="@drawable/item_button"  
         android:orientation="horizontal"
         >          
             <LinearLayout
       android:id="@+id/warning_ll_text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"   
       android:orientation="vertical"   
       android:layout_gravity="center"
       >     
       <TextView
           android:id="@+id/toptextinfo_left"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           style="@style/item_top_left"
           />        
         <TextView
           android:id="@+id/toptextinfo_mr"       
           android:layout_width="280dp"
           android:layout_height="wrap_content"
           style="@style/item_top_mr"
           />     
            </LinearLayout>
         <ImageView 
               android:id="@+id/get_item_detail_warning"  
               android:layout_height="wrap_content"  
               android:layout_width="30dp"   
               android:src="@drawable/next"
               android:layout_gravity="center"          
             >
         </ImageView>
         
             
</LinearLayout>  

activity:

public class testAdapterActivity extends Activity implements commonUi {
 
     String id_projNo="",item_details="",hasQAStr="";
     LinearLayout item_bottom,ld_bottom;
  private ArrayList<HashMap<String, Object>> users_all=new ArrayList<HashMap<String, Object>>();
     private ItemClickListener  itlistener=new ItemClickListener();
RTPullListView view;
myAdapter  myadapter ;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);//获取焦点是,先隐藏键盘
setContentView(R.layout.task_main2);
//date from preview page .
        Bundle b=this.getIntent().getExtras();
        if(b!=null)
        {
            id_projNo=b.getString("id_projNo");
            item_details=b.getString("item_details");
            hasQAStr=b.getString("hasQAStr");
            initDateList(item_details); 
            initUI();  
        }
}
@Override
public void initModel() {
}
@Override
public void initFresh(Message msg) {
}
@Override
public void initUI() {
myadapter=new myAdapter(testAdapterActivity.this,users_all.size());
item_bottom = (LinearLayout)findViewById(R.id.layout1);
ld_bottom= (LinearLayout)getLayoutInflater().inflate(R.layout.approve_item_lv, item_bottom);
view=(RTPullListView)ld_bottom.findViewById(R.id.listviews1);
view.setAdapter(myadapter);
}
public void initDateList(String contextStr)
{
if(contextStr.split("\n").length>=1)
{
String[] task_desc=new String[1];
String[] task_hasQA=new String[1];
for (int i = 0; i < 1; i++) {
HashMap<String, Object>  user = new HashMap<String, Object>();
task_hasQA[i]=hasQAStr;
task_desc[i]=contextStr;
user.put("hasQAStr",task_hasQA[i] );
user.put("task_desc", task_desc[i]);
users_all.add(user);
}
}
}
public class ItemClickListener implements OnItemClickListener 
   {
    public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened   
               View arg1,//The view within the AdapterView that was clicked  
               int arg2,//The position of the view in the adapter  
               long arg3//The row id of the item that was clicked  
               ){   
        Intent intent1=new Intent(testAdapterActivity.this,TrackProblemItemsPage2Activity.class);
intent1.putExtra("id_projNo", id_projNo);
intent1.putExtra("item_details", item_details);
intent1.putExtra("hasQAStr", hasQAStr);
        startActivity(intent1);   
          }
}

public class myAdapter extends BaseAdapter {
private int bottomnum;
private LayoutInflater layoutinflater;
private int index = 0;
private int width=0;
public myAdapter(Context c, int bottomnum) {
this.bottomnum = bottomnum;
this.layoutinflater = LayoutInflater.from(c);
this.width =  ((Activity) c).getWindowManager().getDefaultDisplay().getHeight();  
}


public int getCount() {
return bottomnum;
}
public void SetFocus(int index) {
this.index = index;
// this.notifyDataSetChanged();
this.notifyDataSetInvalidated();// 刷新界面
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}


public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout  ll  = (LinearLayout) layoutinflater.inflate(R.layout.warning_lv_item_layout, null);
              if(ll!=null)
              {
             TextView toptextinfo_left=(TextView)ll.findViewById(R.id.toptextinfo_left);
TextView toptextinfo_mr=(TextView)ll.findViewById(R.id.toptextinfo_mr); 
toptextinfo_left.setText(users_all.get(position).get("hasQAStr").toString());
toptextinfo_mr.setText(users_all.get(position).get("task_desc").toString()); 
   if(hasQAStr.equals("问题交互"))
   {
    view.setOnItemClickListener(itlistener);
   }else
   {
    View next =(View)ll.findViewById(R.id.get_item_detail_warning);
       next.setVisibility(View.GONE);
    LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) view.getLayoutParams(); 
linearParams.width =width;// 当控件的高强制设成75象素
toptextinfo_mr.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件mGrid2
   }
                  }  
return  ll ;
}

    }

}

初始化页面,初始化数据。

public void initUI() {
myadapter=new myAdapter(testAdapterActivity.this,users_all.size());
item_bottom = (LinearLayout)findViewById(R.id.layout1);
ld_bottom= (LinearLayout)getLayoutInflater().inflate(R.layout.approve_item_lv, item_bottom);
view=(RTPullListView)ld_bottom.findViewById(R.id.listviews1);
view.setAdapter(myadapter);
}
public void initDateList(String contextStr)
{
if(contextStr.split("\n").length>=1)
{
String[] task_desc=new String[1];
String[] task_hasQA=new String[1];
for (int i = 0; i < 1; i++) {
HashMap<String, Object>  user = new HashMap<String, Object>();
task_hasQA[i]=hasQAStr;
task_desc[i]=contextStr;
user.put("hasQAStr",task_hasQA[i] );
user.put("task_desc", task_desc[i]);
users_all.add(user);
}
}
}

事件监听事件

public class ItemClickListener implements OnItemClickListener 
   {
    public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened   
               View arg1,//The view within the AdapterView that was clicked  
               int arg2,//The position of the view in the adapter  
               long arg3//The row id of the item that was clicked  
               ){   
        Intent intent1=new Intent(testAdapterActivity.this,TrackProblemItemsPage2Activity.class);
intent1.putExtra("id_projNo", id_projNo);
intent1.putExtra("item_details", item_details);
intent1.putExtra("hasQAStr", hasQAStr);
        startActivity(intent1);   
          }
}

内部类,自定义继承自BaseAdapter.

构造函数中初始化,手机宽,listview数量,Context。


内部类最关键的是实现getView()


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值