我知道这些帖子中有几十个,但即使经过数小时的搜索和尝试不同的解决方案,我也无法使我的列表视图发挥作用。
ListView位于一个片段中,该片段也包含TextView,这也是它不是ListFragment的原因。此片段位于包含Fragment作为Framelayout和Button的Activity中。
ListView正在填充数据,但点击其中一个项目没有任何反应。
活动
public class TurnOnHeadbandActivity extends AppCompatActivity {
private MuseManagerAndroid manager = null;
private final Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.turn_on_headband_activity);
// XX this must come before other libmuse API calls; it loads the
// library.
manager = MuseManagerAndroid.getInstance();
manager.setContext(this);
manager.startListening();
handler.post(tickUi);
}
private final Runnable tickUi = new Runnable() {
@Override
public void run() {
List<Muse> pairedMuses = manager.getMuses();
if (pairedMuses.size() >= 1) {
Fragment connectedFragment = new TurnOnHeadbandConnectedFragment();
PairedMuses pairedMusesObject = new PairedMuses(pairedMuses);
Bundle args = new Bundle();
args.putSerializable("pairedMuses", pairedMusesObject);
connectedFragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.turnOnFragment, connectedFragment);
fragmentTransaction.commit();
}
handler.postDelayed(tickUi, 1000 / 60);
}
};
}
XML for Activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nightsky">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2">
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:id="@+id/turnOnFragment"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@drawable/faqbutton"
android:layout_margin="10dp"
android:textColor="@color/textColor"
android:text="FAQ"/>
</LinearLayout>
片段
public class TurnOnHeadbandConnectedFragment extends Fragment implements OnItemClickListener {
private ListView museListView;
public TurnOnHeadbandConnectedFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.turn_on_headband_connected_fragment, container, false);
Bundle bundle = getArguments();
PairedMuses pairedMuses = (PairedMuses)bundle.getSerializable("pairedMuses");
// Fetch the names and MacAddresses of connected muses and store them in string array
int musesAmount = pairedMuses.getPairedMuses().size();
String[] musesMacAddresses = new String[musesAmount];
for (int i = 0; i < musesAmount; i++) {
musesMacAddresses[i] = String.valueOf(pairedMuses.getPairedMuses().get(i).getName().concat(pairedMuses.getPairedMuses().get(i).getMacAddress()));
}
museListView = (ListView) rootView.findViewById(R.id.museListView);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<>(getActivity(), R.layout.listview_item, musesMacAddresses);
museListView.setAdapter(listViewAdapter);
museListView.setOnItemClickListener(this);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), Activity.class);
intent.putExtra("id", id);
startActivity(intent);
}
}
片段的XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
tools:context="com.musesleep.musesleep.TurnOnHeadbandConnectedFragment"
android:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/textColor"
android:text="Select your headband from the list:"/>
<ListView
android:id="@+id/museListView"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"/>
</LinearLayout>
自定义ListView项目的XML
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:padding="5dp"
android:background="#40D3D3D3"
android:gravity="center_vertical"
android:textColor="@color/textColor"/>