意图不在片段中工作

时间:2015-05-12 10:44:39

标签: android android-intent android-fragments

我有以下代码:

public class HomeFragment extends Fragment {
    private List<RowItem> rowItems;

    private static Integer[] images = { R.drawable.red, R.drawable.spidy,
            R.drawable.prisoners, R.drawable.red, R.drawable.spidy };

    public HomeFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container,
                false);
        ListView lv = (ListView) rootView.findViewById(R.id.myList);
        rowItems = new ArrayList<RowItem>();

        String[] titles = { "Movie1", "Movie2", "Movie3", "Movie4", "Movie5" };
        String[] descriptions = { "First Movie", "Second movie", "Third Movie",
                "Fourth Movie", "Fifth Movie" };
        // Populate the List
        for (int i = 0; i < titles.length; i++) {
            RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
            rowItems.add(item);
        }

        // Set the adapter on the ListView
        LazyAdapter adapter = new LazyAdapter(getActivity()
                .getApplicationContext(), R.layout.list_row, rowItems);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
                Intent myIntent = new Intent(getActivity(), News.class);
                getActivity().startActivity(myIntent);

                // startActivity(in);
            }
        });
        lv.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        return rootView;
    }    
}

我已在项目点击上写了Toast ans startIntent,但它无效。没有吐司显示也没有新的活动开始。我也尝试了onItemSelection方法,但它没有用。我如何在这个Fragment课程中使用它?

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/card_greenborder"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:orientation="vertical"
    android:padding="2dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:text="Dog Tag"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#040404"
        android:textStyle="bold"
        android:typeface="sans" />

    <ImageView
        android:id="@+id/list_image"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_margin="2dp"
        android:layout_weight="0.04"
        android:scaleType="fitXY"
        android:src="@drawable/dhoom" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:text="Create an NFC Pet Tag"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/secondary_text_dark"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnregister"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Register" />

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

首先,不需要为OnItemSelectedListener实施ListView。只需删除它。

第二次制作

android:focusable = "false"
android:focusableInTouchMode = "false"

在listview的自定义虚增视图中UI