作者:手机用户2502916627 | 来源:互联网 | 2023-05-19 11:09
首先普及一下Androidxml布局的三个属性:android:descendantFocusability"afterDescendantsbeforeDescendantsblo
首先普及一下Android xml布局的三个属性:
android:descendantFocusability="afterDescendants/beforeDescendants/blocksDescendants"
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
首先:这三个属性是和textview有关的,button的点击不需要这三个属性
其次:要改变listview的子控件textview或者button的颜色或者文字,需要在写一个接口:
public interface ListItemClickHelp {
void onClick(View item, View widget, int position, int which);
}
接下来直接上代码,说也麻烦其实不难,肯定都能看懂:
import java.util.ArrayList;
import com.example.adapterbuttonclick.R;
import com.shendu.buttonclick.ListItemClickAdapter.ViewHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements ListItemClickHelp {
private TextView mtv_show;
private ListView mlv_show;
private ArrayList mdata = new ArrayList();
private ListItemClickAdapter madapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initLayout();
}
private void initLayout() {
mdata.add("香蕉");
mtv_show = (TextView) this.findViewById(R.id.tv_show);
mlv_show = (ListView) this.findViewById(R.id.lv_show);
madapter = new ListItemClickAdapter(this, mdata, this);
mlv_show.setAdapter(madapter);
mlv_show.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapter, View view,int position, long id) {
mtv_show.setText(mdata.get(position));
}
});
}
@Override
public void onClick(View item, View widget, int position, int which) {
switch (which) {
case R.id.ad_btn_one:
ViewHolder holder = (ViewHolder)item.getTag();
holder.mAd_btn_one.setText(mdata.get(position));
break;
}
}
}