列表视图如何操作列表适配器项目View的对象

时间:2014-01-30 04:12:12

标签: android android-layout android-listview

adapterListView = new SpecialAdapter(getBaseContext(),list,R.layout.listview_layout,from,to);       

 headerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.jpos_ror_header, null, false);

footerView = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.jpos_ror_footer, null, false); 



TextView tin=(TextView)headerView.findViewById(R.id.textViewTinValue);       
TextView textViewNameOfPayor=(TextView)headerView.findViewById(R.id.textViewNameOfPayor);        
TextView textViewLocation=(TextView)headerView.findViewById(R.id.textViewLocation);
  

这是我从页眉和页脚获取对象ID的代码。如何在没有onClick事件的情况下获取列表视图数据适配器的视图。我想在查看布局时进行一些布局操作。

<?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="match_parent"
     android:orientation="horizontal"  >

     <!-- here is my data adapter to be display per item in list view  -->  

    <LinearLayout 
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <RelativeLayout
              android:id="@+id/relativeModeOfPaymentCheck"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="#000000" >

      <LinearLayout
          android:id="@+id/linearForCheck"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"

          android:layout_alignParentLeft="true"
          android:orientation="vertical"
          android:paddingTop="5dp" >

          <TextView
              android:id="@+id/textViewModeOfPaymentCheckValue"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Mode of Payment Value check"

              android:layout_marginLeft="15dp"
              android:textStyle="bold"
              android:textColor="#ffffff" />        
     </LinearLayout>
          <!-- asdasda -->
          <LinearLayout
              android:id="@+id/linearForAmountModeOfPayment"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/linearCheckDate"
              android:orientation="horizontal"
              android:weightSum="2" >

              <RelativeLayout
                  android:id="@+id/relativeBlock100"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="15dp"
                  android:layout_weight="1"
                  android:background="#000000" >

                  <TextView
                      android:id="@+id/textViewModAmountCheck"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="AMOUNT"
                      android:textColor="#ffffff" />
              </RelativeLayout>

              <RelativeLayout
                  android:id="@+id/relativeBlock101"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:background="#000000" >

                  <TextView
                      android:id="@+id/textViewModeOfPayCheckValue"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentTop="true"
                      android:layout_centerHorizontal="true"
                      android:textColor="#ffffff" />
              </RelativeLayout>
          </LinearLayout>
          <!-- name of check issued -->
          <LinearLayout
              android:id="@+id/linearForNameCheckIssued"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/linearForCheck"
              android:orientation="horizontal"
              android:visibility="gone"
              android:weightSum="2" >

              <RelativeLayout
                  android:id="@+id/relativeBlock101"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="15dp"
                  android:layout_weight="1"
                  android:background="#000000" >

                  <TextView
                      android:id="@+id/textViewBankNameCheckIssued"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="RIZAL COMMERCIAL BANKING CORP."
                      android:textColor="#ffffff" />
              </RelativeLayout>
          </LinearLayout>


          <!-- CHECK NUMBER -->
          <LinearLayout
              android:id="@+id/linearCheckNumber"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/linearForNameCheckIssued"
              android:visibility="gone"
              android:orientation="horizontal" >

              <TextView
                  android:id="@+id/textViewCheckNumber"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="15dp"
                  android:text="CHECK # : "
                  android:textColor="#ffffff" />

              <TextView
                  android:id="@+id/textViewCheckNumValue"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="#ffffff" />
          </LinearLayout>
         <!-- CHECK DATE -->
          <LinearLayout
              android:id="@+id/linearCheckDate"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              <!-- android:visibility="gone" -->
              android:layout_below="@+id/linearCheckNumber"
              android:orientation="horizontal" >

              <TextView
                  android:id="@+id/textViewCheckDate"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="15dp"
                  android:text="CHECK DATE : "
                  android:textColor="#ffffff" />

              <TextView
                  android:id="@+id/textViewCheckDateValue"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="#ffffff" />
          </LinearLayout> 


        <LinearLayout
              android:id="@+id/linearLineForCheck"
              android:layout_width="wrap_content"
              android:layout_height="2dp"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/linearForAmountModeOfPayment"
              android:orientation="horizontal" >

                  <View
                      android:id="@+id/View201"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:background="#ffffff" />
          </LinearLayout>  


          </RelativeLayout>
          </LinearLayout>
</LinearLayout>
  

在此适配器中,我希望通过LinearLAyout.setVisibility(LinearLayout.GONE or VISIBLE);

来操纵linearlayouts的视图      

在这里,我想我从himanshu的答案中得到了一些问题。这里是我的listview特殊适配器我有问题如何获取视图和viewGroup实现到我的特殊适配器这里是我的SpecialAdapter ..

public class SpecialAdapter extends SimpleAdapter {
    public static TextView tv;
    private int[] colors = new int[] { 0xffcccccc , 0xffffffff };
    private int[] colors2 = new int[] { 0xffffffff , 0xff000000 }; 
   // Context context;
    public SpecialAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
        super(context, items, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View view = super.getView(position, convertView, parent);
      int colorPos = position % colors.length;
      view.setBackgroundColor(colors[colorPos]);
      //TextView textView = (TextView)view.findViewById(R.id.textViewModAmountCheck);
      //textView.setText("AlvinTest");
      return view;
    }
  

当我实施mg getView2时,能不能告诉我我做对了。

    public View getView2(Context context, int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);//get your layout inflator;

//LayoutInflater inf = LayoutInflater.from(getContext());
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.listview_layout, null);
        }
        LinearLayout yyy = (LinearLayout) convertView.findViewById(R.id.linearCheckDate);
        yyy.setVisibility(View.GONE);
        return convertView;
    }




    public boolean setViewValue(View view, Cursor cursor, int columnIndex){    
        int getIndex = cursor.getColumnIndex("Name");
        String empname = cursor.getString(getIndex);
        tv = (TextView) view;
        tv.setTextColor(Color.WHITE);
        tv.setText(empname);
        if(empname.equals("Any String"))
        {                   
            tv.setTextColor(Color.rgb(58, 58, 224));
            return true;
        }
        return false;           
    }
}
  

这是我从listAdapter

调用getView2时的代码
LayoutInflater inf = LayoutInflater.from(this);
View v = inf.inflate(R.layout.listview_layout, null);

//add magic here bu i don't know how can i get the view of these View and ViewGroup
View itemConvertView = inf.inflate(R.layout.listview_layout, null);
ViewGroup listParent =null; //this.getParent();//(ViewGroup)SelectorView.this.getParent();// null;
adapterListView.getView2(this,0, itemConvertView, listParent);  

3 个答案:

答案 0 :(得分:0)

试试这个: 隐藏您的布局:

YourLayout.setVisibility(View.GONE);

可见您的布局:

YourLayout.setVisibility(View.VISIBLE);

答案 1 :(得分:0)

如果我正确理解了您的问题,您希望以编程方式获取ListView的第一个元素并更改其状态(可见性)。

您可以通过调用getChildAt(int index)方法获取ListView中的元素 例如

LinearLayout yourFirstView = (LinearLayout) listview.getChildAt(0);

访问列表视图第一行中元素的子元素。

ViewType view = (ViewType)yourFirstView.findViewById(R.id.elementId);

设置可见性

yourFirstView.setVisiblity(View.Gone); // or View.VISIBLE

答案 2 :(得分:0)

我从您的问题中了解到,您的ListView项目中有一些视图,您只想在需要时显示该视图。您可以在适配器的getView方法中执行此操作,在该方法中,您可以扩展列表项的自定义布局。像下面的东西

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater layoutInflater = get your layout inflator;

    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.XXXX, null);
    }
    LinearLayout yyy = (LinearLayout) convertView.findViewById(R.id.member_name_text_view);
    yyy.setVisibility(View.Gone)
    return convertView;
}