3

リストビューアイテムをタッチしたときに強調表示し、強調表示したままにする必要があります。見つけたものをすべて試しましたが、何も機能しませんでした。これが私のコードです:

これはリストビューです:

<ListView
    android:id="@+id/lvUsers"
    android:layout_width="300dp"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:background="@drawable/border2"
    android:choiceMode="singleChoice"
    android:padding="5dp" >
</ListView>

これ@drawable/border2は、リストビューの境界線にすぎません。

これはリストビューアイテムのレイアウトです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutUsersRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_selector_background"
android:orientation="vertical" >

<TextView
    android:id="@+id/tvAllUsersName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="User name"
    android:textAppearance="?android:attr/textAppearanceLarge" />

これはlist_selector_backgroundです:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@color/blue" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/blue" /> <!-- focused and pressed-->
    <item android:state_pressed="true" android:drawable="@color/blue" /> <!-- pressed -->
    <item android:drawable="@color/transparent" /> <!-- default -->
</selector>

これはリストビューアダプタからのコードです

public class UsersAdapter extends BaseAdapter implements OnClickListener {

LoginActivity context;
private List<String> listOfUsers;

public UsersAdapter(LoginActivity _context, List<String> listOfUsers) {
    context = _context;
    this.listOfUsers = listOfUsers;
}

public int getCount() {
    return listOfUsers.size();
}

public Object getItem(int position) {
    return listOfUsers.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    String entry = listOfUsers.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.users_row, null);
    }

    TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
    UserName.setText(entry);

    LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
    LinLayout.setFocusableInTouchMode(false);
    LinLayout.setFocusable(false);
    LinLayout.setOnClickListener(this);

    return convertView;
}

public void onClick(View v) {
    // get the row the clicked button is in
    LinearLayout row = (LinearLayout) v;

    TextView child = (TextView) row.getChildAt(0);
    if (child.getText().toString().equals("Admin")) {
        Intent i = new Intent("com.vorteksed.checkinform.ADMINLOGINACTIVITY");
        i.putExtra("verificationFor", "ADMIN_LOGIN");
        context.startActivity(i);
    } else {
        context.userClicked(child.getText().toString());

        // focus the pin field after selecting user name from the list
        if (context.currentList != null) {
            context.etLoginPin.setVisibility(View.VISIBLE);
            context.tvPin.setVisibility(View.VISIBLE);
            context.etLoginPin.requestFocus();
            context.etLoginPin.setText("");
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(context.etLoginPin, 0);
        }
    }
}
}

これは機能していません。押している間だけアイテムを強調表示します。リリースするとすぐにハイライトが消えます。

ここに解決策があります

android:background="@drawable/list_selector_background"リストビューアイテムのレイアウトから行を削除します。セレクターは一切必要ありません。そして、これがアダプターのコードです:

public class UsersAdapter extends BaseAdapter {

LoginActivity context;
private List<String> listOfUsers;
boolean[] arrBgcolor;
private int blue = Color.BLUE;
private int transparent = Color.TRANSPARENT;

public UsersAdapter(LoginActivity _context, List<String> listOfUsers) {
    context = _context;
    this.listOfUsers = listOfUsers;

    arrBgcolor = new boolean[listOfUsers.size()];
    resetArrbg();

}

private void resetArrbg() {
    for (int i = 0; i < arrBgcolor.length; i++) {
        arrBgcolor[i] = false;                  
    }
}

public int getCount() {
    return listOfUsers.size();
}

public Object getItem(int position) {
    return listOfUsers.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    String entry = listOfUsers.get(position);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.users_row, null);
    }


    TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
    UserName.setText(entry);

    LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
    LinLayout.setFocusableInTouchMode(false);
    LinLayout.setFocusable(false);
    LinLayout.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            handleClick(v, position);
        }
    });

    if (arrBgcolor[position])
        LinLayout.setBackgroundColor(blue);
    else 
        LinLayout.setBackgroundColor(transparent);

    return convertView;
}

public void handleClick(View v, int position) {
    LinearLayout row = (LinearLayout) v;

    resetArrbg();
    arrBgcolor[position] = true;
    notifyDataSetChanged();

    TextView child = (TextView) row.getChildAt(0);
    if (child.getText().toString().equals("Admin")) {
        Intent i = new Intent("com.vorteksed.checkinform.ADMINLOGINACTIVITY");
        i.putExtra("verificationFor", "ADMIN_LOGIN");
        context.startActivity(i);
    } else {
        context.userClicked(child.getText().toString());

        // focus the pin field after selecting user name from the list
        if (context.currentList != null) {
            context.etLoginPin.setVisibility(View.VISIBLE);
            context.tvPin.setVisibility(View.VISIBLE);
            context.etLoginPin.requestFocus();
            context.etLoginPin.setText("");
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(context.etLoginPin, 0);
        }
    }
}

}
4

2 に答える 2

1

セレクターを削除して、次のことを試してください。

編集

public class UsersAdapter extends BaseAdapter implements OnClickListener {

        LoginActivity context;
        private List<String> listOfUsers;
        boolean[] arrBgcolor;
        private int blue = Color.BLUE;

        public UsersAdapter(LoginActivity _context, List<String> listOfUsers) {
            context = _context;
            this.listOfUsers = listOfUsers;

            arrBgcolor = new boolean[listOfUsers.size()];
            resetArrbg();

        }

        private void resetArrbg() {
            for (int i = 0; i < arrBgcolor.length; i++) {
                arrBgcolor[i] = false;                  
            }
        }

        ......

        public View getView(final int position, View convertView, ViewGroup parent) {
            String entry = listOfUsers.get(position);
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(R.layout.users_row, null);
            }


            TextView UserName = (TextView) convertView.findViewById(R.id.tvAllUsersName);
            UserName.setText(entry);

            LinearLayout LinLayout = (LinearLayout) convertView.findViewById(R.id.layoutUsersRow);
            LinLayout.setFocusableInTouchMode(false);
            LinLayout.setFocusable(false);
            LinLayout.setOnClickListener(this);

            //add the following, the position is accessible from here
            if (arrBgcolor[position]) {
                convertView.setBackgroundColor(blue);
            }
            convertView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    resetArrbg();
                    arrBgcolor[position] = true;
                    notifyDataSetChanged();
                }
            }); 
            return convertView;
        }
于 2012-12-04T10:37:34.283 に答える
0

//// @ drawable / list_selector

 <selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:drawable="@drawable/list_item_bg_normal"
  android:state_pressed="false" android:state_selected="false" android:state_activated="false"/>

  <item android:drawable="@drawable/list_item_touch"
  android:state_pressed="true"/>

 <item android:drawable="@drawable/list_item_touch"
 android:state_pressed="false" android:state_selected="true"/>

 <item android:drawable="@drawable/list_item_bg_pressed"
  android:state_activated="true"/>

</selector>

////////////////////およびListView

        <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector"
        android:background="@color/list_background"
        android:divider="#060606"/>

///////////////////////listviewアイテム

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@drawable/list_selector">

      <ImageView
      android:id="@+id/icon"
      android:layout_width="35dp"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginLeft="12dp"
      android:layout_marginRight="12dp"
      android:contentDescription="@string/desc_list_item_icon"
      android:src="@drawable/ic_home"
      android:layout_centerVertical="true" />

      </RelativeLayout>
于 2014-10-11T16:23:12.193 に答える