点击 ListView 外的button 聚焦到 listview 里item

fajiuzhishan 发布于 2016/06/01 11:11
阅读 199
收藏 1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">
     
        <LinearLayout 
            android:id="@+id/infor_layout1"
            android:layout_width="fill_parent"
            android:layout_height="102px"
            android:orientation="horizontal"
            android:paddingTop="24px"
            android:background="#f6f6f6"
            android:paddingBottom="24px"
            android:paddingLeft="24px"
            android:paddingRight="24px"
            android:focusable="true"
            android:focusableInTouchMode="true"
            >
            <Button
                android:id="@+id/infor_btn1" 
                android:layout_width="100px"
                android:layout_height="56px"   
                android:textColor="#fff"
                android:gravity="center" 
                android:background="@color/infor_button1"/>
            <Button 
                android:id="@+id/infor_btn2" 
                android:gravity="center"
                android:textSize="22px"  
                android:layout_marginLeft="50px"
                android:layout_width="100px"
                android:layout_height="56px"   
                android:background="@color/infor_button2"/>
            <Button 
                android:id="@+id/infor_btn3" 
                android:gravity="center"
                android:textSize="22px" 
                android:layout_marginLeft="50px"
                android:layout_width="100px"
                android:layout_height="56px"   
                android:background="@color/infor_button3"/>
            <Button 
                android:id="@+id/infor_btn4" 
                android:gravity="center"
                android:textSize="22px" 
                android:layout_marginLeft="50px"
                android:layout_width="100px"
                android:layout_height="56px"  
                android:background="@color/infor_button4" />
            <Button
                android:id="@+id/infor_btn5" 
                android:gravity="center"
                android:textSize="22px" 
                android:layout_marginLeft="50px"
                android:layout_width="100px"
                android:layout_height="56px"  
                android:background="@color/infor_button5"/>
            
        </LinearLayout>
        
        <com.example.demo.view.MyListView 
                android:id="@+id/gView4" 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:paddingLeft="24px"
                android:paddingRight="24px" 
                android:gravity="center"
                android:background="#f6f6f6" 
                android:scrollbars="none"> 
         </com.example.demo.view.MyListView>
         
</LinearLayout>

listview 的布局
<?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="fill_parent"
    android:orientation="vertical" 
    android:background="#f6f6f6">
    <TextView 
          android:layout_width="fill_parent"
          android:layout_height="78px"
          android:text="IOS9人机界面指南(三):IOS技术(上)"
          android:textSize="32px"
		  android:layout_marginTop="24px"
	      android:layout_marginBottom="24px"/>
    <ImageView 
        android:id="@+id/iamge4"
        android:layout_width="702px"
        android:layout_height="400px"
        
        />
    <LinearLayout 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"  
                android:gravity="center_vertical"
                android:paddingTop="24px" 
                android:paddingBottom="24px">
                <TextView 
                    android:id="@+id/text4"
                    android:layout_width="100px"
                    android:layout_height="56px"
                    android:textSize="22px" 
                    android:gravity="center"
                    android:textColor="#fff"
                    android:background="@color/infor_button2"/>
                
                <TextView
                    android:id="@+id/text5" 
                    android:textColor="#737373"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent" 
                    android:gravity="center" 
                    android:layout_marginLeft="24px"/> 
               
            </LinearLayout>
       
</LinearLayout>



public class FragmentInfor extends Fragment  implements OnClickListener{
	 
	 
	private ListView listView1;
	private SimpleAdapter sim4_Adapter;
	private List<Map<String, Object>>    data_list4;
	private String[] iconName4={"设计","设计""设计","设计""设计","设计"
                                   "设计","设计""设计","设计"};
	private int[] icon4={R.drawable.infor_image,R.drawable.infor_image,
			R.drawable.infor_image,R.drawable.infor_image,R.drawable.infor_image,
			R.drawable.infor_image,R.drawable.infor_image,R.drawable.infor_image,};
	private String[] time={"2016-5-30","2016-5-30","2016-5-30","2016-5-30",
			"2016-5-30","2016-5-30","2016-5-30","2016-5-30"}; 
	 
	 
	private Button  btn1,btn2,btn3,btn4,btn5;
	 
	 
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) { 
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_infor, container, false);
		
		initView(view);
		getData_list4(view); 
		return view;
	}
	private void initView(View view) {
		 btn1=(Button) view.findViewById(R.id.infor_btn1);
		 btn2=(Button) view.findViewById(R.id.infor_btn2);
		 btn3=(Button) view.findViewById(R.id.infor_btn3);
		 btn4=(Button) view.findViewById(R.id.infor_btn4);
		 btn5=(Button) view.findViewById(R.id.infor_btn5);
		 
		 btn1.setOnClickListener(this); 
		 btn2.setOnClickListener(this);
		 btn3.setOnClickListener(this);
		 btn4.setOnClickListener(this);
		 btn5.setOnClickListener(this);
	}
	 
	 public List<Map<String, Object>> getData_list4(View view) {
		 listView1=(ListView) view.findViewById(R.id.gView4);
		 data_list4=new ArrayList<Map<String,Object>>(); 
		 for(int i=0;i<icon4.length;i++){
			 Map<String, Object> map=new HashMap<String, Object>();
			 map.put("image", icon4[i]);
			 map.put("text", iconName4[i]); 
			 map.put("time", time[i]); 
			 map.put("count1", count1[i]); 
			 map.put("count2", count2[i]); 
			 map.put("count3", count3[i]); 
			 
			 data_list4.add(map);
		 }
		 
		 String[] from={"image","text","time"};
		 int[] to={R.id.iamge4,R.id.text4,R.id.text5};
		 sim4_Adapter=new SimpleAdapter(getActivity(), data_list4, R.layout.infor_list_item, from, to);
		 listView1.setAdapter(sim4_Adapter); 
		 return data_list4;
		 
		
	} 
	 	 
	@Override
	public void onClick(View v) {
		 switch(v.getId()){
		 case R.id.infor_btn2:  
			  
		 }
		
	}

	 
	 
}

当点击  btn2 时,要聚焦到 listview 中的第三个元素






加载中
0
花花兽
f
fajiuzhishan
这个是获取历史数据吧?没有历史数据是不是就找不到?
花花兽
花花兽
回复 @fajiuzhishan : 太白了不适合看这个,去看基础知识吧
f
fajiuzhishan
就是不知道怎么用啊,小白一个。给点具体的代码啊
0
陈陈陈陈陈陈
点击事件响应后:listView.setSelection(savedPosition);
返回顶部
顶部