android 小部件中的小部件未按预期工作
android widget within a widget not working as intended
我使用 FrameLayout 作为自定义列表项。我为 listView 设置了一个 onItemClickListener,但我的每个项目本身都有一个按钮,虽然它们中的按钮是可点击的,但项目本身不是,它们应该做不同的事情。如何使项目及其中的按钮都可点击?
代码如下:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ProjectCreateScreen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout1);
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
Button btn = (Button) findViewById(R.id.addBtn);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
((ArrayAdapter) addAdapter).notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent switchToEdit = new Intent(ProjectCreateScreen.this,
teamCreateScreen.class);
startActivity(switchToEdit);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
自定义listView项xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/listFrame"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="20sp"
/>
<Button android:id="@+id/frameBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
activity的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/AddProject"
android:id="@+id/addBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/noProjectsNotice"
android:id="@+id/NOPROJECT"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="16sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/addBtn"
android:id="@+id/lv"
android:visibility="invisible">
</ListView>
</RelativeLayout>
为您希望在列表视图中可点击的任何视图添加以下两个属性:
android:focusable="false"
android:focusableInTouchMode="false"
这应该可以解决大多数情况。如果这还不够,还要将以下属性添加到列表视图布局的根布局中
android:descendantFocusability="blocksDescendants"
我使用 FrameLayout 作为自定义列表项。我为 listView 设置了一个 onItemClickListener,但我的每个项目本身都有一个按钮,虽然它们中的按钮是可点击的,但项目本身不是,它们应该做不同的事情。如何使项目及其中的按钮都可点击?
代码如下:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class ProjectCreateScreen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout1);
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
Button btn = (Button) findViewById(R.id.addBtn);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
((ArrayAdapter) addAdapter).notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent switchToEdit = new Intent(ProjectCreateScreen.this,
teamCreateScreen.class);
startActivity(switchToEdit);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
自定义listView项xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/listFrame"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:textSize="20sp"
/>
<Button android:id="@+id/frameBtn"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
activity的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl">
<Button
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text="@string/AddProject"
android:id="@+id/addBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/noProjectsNotice"
android:id="@+id/NOPROJECT"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="16sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/addBtn"
android:id="@+id/lv"
android:visibility="invisible">
</ListView>
</RelativeLayout>
为您希望在列表视图中可点击的任何视图添加以下两个属性:
android:focusable="false"
android:focusableInTouchMode="false"
这应该可以解决大多数情况。如果这还不够,还要将以下属性添加到列表视图布局的根布局中
android:descendantFocusability="blocksDescendants"