问题描述
我想强调我的ListView的第一行(0)当我创建列表视图。我尝试不同的方法(例如,你可以在评论code见),但是毫无效果。这很奇怪,因为在OnItemClickListener的亮点正常工作! (它的工作原理通过一个XML选择器)。
我有这个方法在我的片段,这就是所谓的通过onCreateView:
私人无效createListViewAll(查看rootView,INT listID,字符串[] []文本清单)
{
MainListAdapter bindingData =新MainListAdapter(getActivity(),文本清单,真正的);
ListView控件列表=(ListView控件)rootView.findViewById(listID);
list.setAdapter(bindingData);
HelperListView.getListViewSize(名单);
// list.setItemsCanFocus(真正的);
// list.setItemChecked(0,真正的);
// list.setSelection(0);
// list.setSelected(真正的);
// list.requestFocus();
//
// list.setSelection(0);
// list.setSelected(真正的);
// list.getChildAt(list.getHeaderViewsCount() - list.getFirstVisiblePosition()).setSelected(真正的);
// list.getChildCount();
// bindingData.getCount();
//查看的listItem = bindingData.getView(0,空,清单);
// listItem.setActivated(真正的);
// listItem.setSelected(真正的);
// list.invalidate();
// list.isItemChecked(0);
currentView = bindingData.getView(0,空,清单);
// Click事件单列表行
list.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长ID)
{
mItemSelected.onItemSelected(位置);
highlightView(视图);
}
});
}
私人无效highlightView(查看视图)
{
如果(查看!= currentView)
{
如果(currentView!= NULL)
{
currentView.isSelected();
currentView.setBackgroundResource(R.drawable.gradient_bg);
currentView.setSelected(假);
//currentView.invalidate();
}
view.setSelected(真正的);
currentView =图。
}
}
(currentView只是一个静态变量)
在getView在listAdapter:
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看VI = convertView;
如果(convertView == NULL)
{
VI = inflater.inflate(R.layout.list_row,NULL);
持有人=新ViewHolder();
holder.titleRow =(TextView中)vi.findViewById(R.id.titleRow);
holder.subtitleRow =(TextView中)vi.findViewById(R.id.subtitleRow);
vi.setTag(保持器);
}
其他{
支架=(ViewHolder)vi.getTag();
}
如果(位置== 0安培;&安培; highlightFirstElement)
{
vi.setSelected(真正的);
vi.setBackgroundResource(R.drawable.gradient_bg_hover);
}
如果(文本清单!= NULL)
{
holder.titleRow.setText(文本清单[0] [位置]);
holder.subtitleRow.setText(文本清单[1] [位置]);
}
其他
{
holder.titleRow.setText(参数[idSubject.get(位置)]);
holder.subtitleRow.setText(subTitle.get(位置));
}
返回六;
}
列表视图的适配器类在getView方法,采取文本ID从布局文件和应用背景颜色给它。
例如,
公开查看getView(INT位置,查看convertView,ViewGroup中父){
视图V = NULL;
字符串充气= Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater力=(LayoutInflater)my_ctx
.getSystemService(充气);
V = li.inflate(R.layout.layoutfile,父母,假);
如果(位置== 0)
{
TextView的文字=(TextView的)findViewById(R.id.textView1);
text.setBackgroundColor(Color.ANY_COLOR);
}
}
I would like to highlight the first row (0) of my ListView when I create the listView. I tried different ways (like you can see in the commented code), but nothing worked.It's strange because the highlight in the OnItemClickListener WORKS FINE! (It works via an xml selector).
I have this method in my fragment that it's called by onCreateView:
private void createListViewAll(View rootView, int listID, String[][] textList)
{
MainListAdapter bindingData = new MainListAdapter(getActivity(), textList, true);
ListView list = (ListView) rootView.findViewById(listID);
list.setAdapter(bindingData);
HelperListView.getListViewSize(list);
// list.setItemsCanFocus(true);
// list.setItemChecked(0, true);
// list.setSelection(0);
// list.setSelected(true);
// list.requestFocus();
//
// list.setSelection(0);
// list.setSelected(true);
// list.getChildAt( list.getHeaderViewsCount() - list.getFirstVisiblePosition() ).setSelected(true);
// list.getChildCount();
// bindingData.getCount();
// View listItem = bindingData.getView(0, null, list);
// listItem.setActivated(true);
// listItem.setSelected(true);
// list.invalidate();
// list.isItemChecked(0);
currentView = bindingData.getView(0, null, list);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
mItemSelected.onItemSelected(position);
highlightView(view);
}
});
}
private void highlightView(View view)
{
if (view != currentView)
{
if (currentView!=null)
{
currentView.isSelected();
currentView.setBackgroundResource( R.drawable.gradient_bg );
currentView.setSelected(false);
//currentView.invalidate();
}
view.setSelected(true);
currentView = view;
}
}
(currentView is just a static variable)
the getView in the listAdapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
{
vi = inflater.inflate(R.layout.list_row, null);
holder = new ViewHolder();
holder.titleRow = (TextView)vi.findViewById(R.id.titleRow);
holder.subtitleRow = (TextView)vi.findViewById(R.id.subtitleRow);
vi.setTag(holder);
}
else{
holder = (ViewHolder)vi.getTag();
}
if (position==0 && highlightFirstElement)
{
vi.setSelected(true);
vi.setBackgroundResource( R.drawable.gradient_bg_hover );
}
if (textList!=null)
{
holder.titleRow.setText(textList[0][position]);
holder.subtitleRow.setText(textList[1][position]);
}
else
{
holder.titleRow.setText( arguments[idSubject.get(position)] );
holder.subtitleRow.setText(subTitle.get(position));
}
return vi;
}
In getView method of listview's adapter class,take id of text from Layout file and apply background color to it.
for example,
public View getView(int position, View convertView, ViewGroup parent) {
View v = null;
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater) my_ctx
.getSystemService(inflater);
v = li.inflate(R.layout.layoutfile, parent, false);
if(position==0)
{
TextView text = (TextView) findViewById(R.id.textView1);
text.setBackgroundColor(Color.ANY_COLOR);
}
}
这篇关于通过code选中创建时ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!