我做了一个InfoscreenActivity,它会在我的应用程序中显示一个"设置/关于"页面(包含应用程序版本信息,许可证,联系方式,设置和备份选项)。该页面上的项目是在ListView中使用名为InfoscreenAdapter的自定义ArrayAdapter生成的。
下面是相关的类和xml布局文件。
InfoscreenActivity:
public class InfoscreenActivity extends ActionBarActivity {
private static final String TAG = "InfoscreenActivity";
private InfoscreenItem appVersion;
private InfoscreenItem iiLicenses;
private InfoscreenItem contactUs = new InfoscreenItem("Contact us");
private InfoscreenItem iiSetup = new InfoscreenItem("Launch Setup");
private InfoscreenItem iiBackup = new InfoscreenItem("Perform Backup");
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
if(AppConfig.DEBUG) Log.i(TAG, "launching onCreate() ...");
String appVersionString;
if(AppConfig.DEBUG) Log.i(TAG, "trying to fetch the version name ...");
try {
appVersionString = SetupActivity.getVersionName(this);
appVersion = new InfoscreenItem("App version", appVersionString);
} catch (Exception e) {
if(AppConfig.DEBUG) Log.e(TAG, e.getMessage());
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_infoscreen);
iiLicenses = new InfoscreenItem(getResources().getString(R.string.title_activity_licenses));
// Get ListView object from xml
lv = (ListView) findViewById(R.id.infoscreenListViewID);
// Defined Array values to show in ListView
InfoscreenItem[] items = new InfoscreenItem[] {appVersion, contactUs, iiLicenses, iiSetup, iiBackup};
// Define a new InfoscreenAdapter
InfoscreenAdapter adapter = new InfoscreenAdapter(this, items);
// Assign adapter to ListView
if(AppConfig.DEBUG) Log.i(TAG, "setting adapter");
lv.setAdapter(adapter);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
if(AppConfig.DEBUG) Log.i(TAG, "item " + Integer.toString(position) + " clicked.");
// for the "Contact us" item
if(position == 1) {
TextView clickedView = (TextView) view.findViewById(R.id.textView);
onContactClick(clickedView);
}
// for the "Licenses" item
if(position == 2) {
TextView clickedView = (TextView) view.findViewById(R.id.textView);
onLicensesClick(clickedView);
}
// for the "Setup" item
if (position == 3) {
if (AppConfig.DEBUG) Log.d(TAG, "launching Setup Activity...");
startActivity(new Intent(getApplication(), SetupActivity.class));
}
// for the "Backup" item
if (position == 4) {
Intent iBackup = new Intent(getApplication(), BackupAndRestoreActivity.class);
iBackup.putExtra("MODE", 1);
startActivity(iBackup);
}
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(AppConfig.DEBUG) Log.i(TAG, "exiting onCreate() ...");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if(AppConfig.DEBUG) Log.i(TAG, "onCreateOptionsMenu() ...");
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_infoscreen, menu);
if(AppConfig.DEBUG) Log.i(TAG, "exiting onCreateOptionsMenu() ...");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
else if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onContactClick(View v) {
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{"contact@gmail.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "(subject)");
email.putExtra(Intent.EXTRA_TEXT, "(message)");
email.setType("message/rfc822");
startActivity(Intent.createChooser(email, "Choose an Email client :"));
}
public void onLicensesClick(View v) {
if(AppConfig.DEBUG) Log.i(TAG, "onLicensesClick() ...");
startActivity(new Intent(InfoscreenActivity.this, LicensesActivity.class));
}
}
适配器InfoscreenAdapter:
public class InfoscreenAdapter extends ArrayAdapter<InfoscreenItem> {
private static final String TAG = "InfoscreenAdapter";
private Context context;
private InfoscreenItem[] infoscreenItems;
// constructor
public InfoscreenAdapter(Context context, InfoscreenItem[] infoscreenItems) {
super(context, -1, infoscreenItems);
if(AppConfig.DEBUG) Log.i(TAG, "entering InfoscreenAdapter constructor");
this.context = context;
this.infoscreenItems = infoscreenItems;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(AppConfig.DEBUG) Log.i(TAG, "fetching View " + Integer.toString(position) +
". Title: " + infoscreenItems[position].getItemTitle() + ", name: " + infoscreenItems[position].getItemName());
// the infoscreenItem has only 1 line (title)
if(infoscreenItems[position].getItemName().equals("")) {
// inflate the Layout from the infoscreen_single_line.xml
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_infoscreen_single_line, parent, false);
// get the TextView from the layout
TextView tv = (TextView) convertView.findViewById(R.id.textView);
//set its text
tv.setText(infoscreenItems[position].getItemTitle());
return convertView;
}
// the infoscreenItem has 2 lines (title & name)
else {
// inflate the Layout from the infoscreen_dual_line xml
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.item_infoscreen_dual_line, parent, false);
// get the TextView for the title
TextView appVersionTextView = (TextView) convertView.findViewById(R.id.textView_title);
//set its text
appVersionTextView.setText(infoscreenItems[position].getItemTitle());
// get the TextView for the version name
TextView appVersionNameTextView = (TextView) convertView.findViewById(R.id.textView_versionName);
//set its text
appVersionNameTextView.setText(infoscreenItems[position].getItemName());
return convertView;
}
}
}
InfoscreenItem类:
public class InfoscreenItem {
private String itemTitle;
private String itemName;
// constructors
public InfoscreenItem () {
itemTitle = "";
itemName = "";
}
public InfoscreenItem (String iTitle){
itemTitle = iTitle;
itemName = "";
}
public InfoscreenItem (String iTitle, String iName){
itemTitle = iTitle;
itemName = iName;
}
public String getItemTitle() {
return itemTitle;
}
public String getItemName() {
return itemName;
}
}
用于格式化的xml文件。
activity_infoscreen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="at.dimi3.crew2go.InfoscreenActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/infoscreenListViewID"
android:clickable="true" />
</LinearLayout>
item_infoscreen_single_line.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:clickable="true" />
</LinearLayout>
item_infoscreen_dual_line.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView_title"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="5dp"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView_versionName"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:clickable="true" />
</LinearLayout>
现在一切都如预期的那样工作,除了在"Infoscreen"的ListView中,项目只能在文本旁边点击。有没有办法让整个ListView项目对点击做出反应,不管你在哪里点击项目?我错过了什么在xml布局模板?我试过设置android:clickable="true",删除它,周围改变android:layout_width="wrap_content"到match_parent或fill_parent(如建议在其他类似的线程-那些是不可点击的虽然,所以不完全相同的问题,因为我有)。
在你的item_infoscreen_dual_line.xml
或item_infoscreen_single_line.xml
(我不知道哪一个导致你的麻烦)给你的linearLayout一个id。现在找到该布局并在该布局上设置OnClickListener
很简单。你已经给你的textview填充。但是你想要点击整个LinearLayout项目。这样就不用给TextView写padding了,给LinearLayout写
好的,非常感谢您的输入。做了更多的谷歌搜索,我偶然发现了下面的例子,并在我的Android Studio中创建了这个确切的项目。点击在这个例子中无处不在,所以我开始逐个删除xml属性,以匹配示例中的xml,事实证明,删除android:clickable="true"解决了这个问题。我有点困惑(这听起来自相矛盾),但它是有效的。