gpt4 book ai didi

android - TextView 阻止单击 ListView

转载 作者:行者123 更新时间:2023-11-29 01:38:45 25 4
gpt4 key购买 nike

我有一个 ListView 和一个创建项目列表的自定义 ArrayAdapter。我正在努力做到这一点,因此单击列表项上的任意位置都会执行某些操作。然而,唯一可点击的区域在文本区域之外。

红色的左、右、上之外是可点击的。

http://i.stack.imgur.com/CJuLm.png

主要 Activity XML:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffd7d7d7">
<!-- The main content view -->

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<ListView
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/cities_list"
android:drawSelectorOnTop="true"
android:divider="@android:color/transparent"
android:smoothScrollbar="true"
android:descendantFocusability="blocksDescendants"/>

</FrameLayout>


<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="4dp"
android:background="#ffd7d7d7"/>

</android.support.v4.widget.DrawerLayout>

项目 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:clickable="false"
android:focusable="false"
>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="2dp"
android:showDividers="beginning"
android:background="@drawable/city_list_item_background"
android:baselineAligned="true"
android:padding="8dp"
android:clickable="false"
android:focusable="false"
>


<TextView
android:id="@+id/tvCity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="City"
android:textSize="24sp"
android:clickable="false"
android:focusable="false"
android:background="#FF0000"
android:enabled="false"
android:editable="false"
android:textColor="#000000" />

<TextView
android:id="@+id/tvCountry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Country"
android:clickable="false"
android:focusable="false"
android:background="#FF0000"/>

</LinearLayout>
</RelativeLayout>

适配器类:

public class CitiesAdapter extends ArrayAdapter<NewCity> {
public CitiesAdapter(Context context, ArrayList<NewCity> cities) {
super(context, R.layout.item_city, cities);
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {




// get the data item for this position
NewCity cities = getItem(position);

// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_city, parent, false);
}

// lookup for data population
TextView tvCity = (TextView) convertView.findViewById(R.id.tvCity);

TextView tvCountry = (TextView) convertView.findViewById(R.id.tvCountry);

// populate the data into the template view using the ata object
tvCity.setText(cities.city);
tvCountry.setText(cities.country);




// return the completed view to render on screen
return convertView;

}



@Override
public boolean areAllItemsEnabled() {
return true;
}

@Override
public boolean isEnabled(int arg0) {
return true;
}
}

主要 Activity :

public class MainActivity
extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mMenuItems;

public static class MyPlacesFragment
extends Fragment {

public MyPlacesFragment() {

// Empty constructor required for fragment subclasses
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_places, container, false);
String menu_title = getResources().getStringArray(R.array.menu_items)[0];

getActivity().setTitle(menu_title);
return rootView;
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();

//Fragment f0 = new MyPlacesFragment();
//ft.replace(R.id.content_frame, f0);
//ft.commit();

populateCitiesList();


mTitle = mDrawerTitle = getTitle();
mMenuItems = getResources().getStringArray(R.array.menu_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);

// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mMenuItems));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}

public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);

if (savedInstanceState == null) {
selectItem(0);
}
}

private void populateCitiesList() {
// Construct the data source
ArrayList<NewCity> arrayOfUsers = NewCity.getUsers();
// Create the adapter to convert the array to views
CitiesAdapter adapter = new CitiesAdapter(this, arrayOfUsers);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.cities_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener( new AdapterView.OnItemClickListener()
{

@Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id)
{
//Tweet theTweet = (Tweet)parent.getAdapter().getItem(position);
//saved.insert(theTweet);
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_LONG).show();
}
} );
}

如有任何建议,我们将不胜感激。

最佳答案

我认为您需要从 listView 中删除 android:descendantFocusability="blocksDescendants"。如果我理解正确,我相信它会阻止 textview 获得焦点/点击并将其传递给父级。也许尝试将其设置为“beforeDescendants”。

http://developer.android.com/reference/android/view/ViewGroup.html#FOCUS_BEFORE_DESCENDANTS

如果找不到适合您的解决方案,一种解决方法是将 OnClickListener 设置为执行与 ListView 相同功能的 TextView 。

关于android - TextView 阻止单击 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com