对listview活动进行分段

时间:2017-03-30 05:16:45

标签: android xml listview android-activity fragment

我对Android开发很新,所以请不要因为愚蠢的问题/错误而惹恼我!

我的活动中有一个列表视图,但是当我点击列表视图中的一个框架时,我想要打开一个片段。我有onclick工作,但它没有启动新活动。

我将在下面提供我的代码。

这是oncreate

protected void onCreate (Bundle savedInstanceState)
{
    //Fragment fragment_blank2=new SomeFragment();
    e = myBadData.getData();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.event_list);
    events=(ListView) findViewById(R.id.dayList);
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,e);
    events.setAdapter(adapter);
    events.setOnItemClickListener(this);
}

这是onclick

public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    myBadData.setId(i);
    Fragment fr = new event_description();

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();

    fragmentTransaction.add(R.id.eventdescription, fr);
}

这是片段的XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id = "@+id/eventdescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="layout.BlankFragment">

<!-- TODO: Update blank fragment layout -->


    <TextView
        android:layout_width="match_parent"
        android:layout_height="137dp"
        android:text="@string/event_name"
        android:textSize="26sp"
        android:layout_gravity="top"
        android:gravity="top|center"
        android:paddingTop="10dp"
        android:id="@+id/textView"
        android:background="@android:color/holo_orange_light"
        android:textColor="@android:color/black"
        android:layout_weight="1.08" />
    <TextView
        android:text="@string/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/date"
        android:layout_marginTop="@dimen/activity_top_margin"
        android:layout_marginLeft="@dimen/activity_side_margin"
        android:textSize="18sp"
        android:drawableTint="@android:color/background_dark" />
    <TextView
        android:text="@string/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/time"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="@dimen/activity_side_margin"
        android:textSize="18sp" />
    <TextView
        android:text="@string/cost"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cost"
        android:layout_marginTop="@dimen/activity_top_margin"
        android:layout_marginRight="70dp"
        android:textSize="18sp"/>
    <TextView
        android:text="@string/address"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/address"
        android:layout_marginTop="70dp"
        android:layout_marginRight="60dp"
        android:textSize="18sp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="135dp"
        android:layout_marginLeft="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <TextView
                android:id="@+id/tv_long"
                android:paddingTop="10dp"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="@string/sample_text"
                android:textSize="18sp"
                android:layout_weight="1.08">
            </TextView>
        </LinearLayout>
    </ScrollView>

    <Button
        android:text="@string/add_to_calendar"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:id="@+id/button2"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="95dp"/>
</FrameLayout>

这是片段的类

public class event_description extends Fragment
{
    @Nullable
    //@Override
    public View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate (R.layout.event_description, container, false);
        Bundle args = getArguments();
        String arg = args.getString(eventList.KEY_NAME);
        //return super.(R.layout.event_description, container, false);
        return view;
    }

    public View getView() {
      return getView();
    }
}

1 个答案:

答案 0 :(得分:0)

适用于您的活动XML

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- TODO: Update blank fragment layout -->

    <ListView
        android:id="@+id/dayList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

   </FrameLayout>

并在FramgmentTransaction中传递Activity的容器

public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l)
{
    myBadData.setId(i);
    Fragment fr = new event_description();

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();


    //Your Container of Activity
    fragmentTransaction.add(R.id.my_frame, fr);

    fragmentTransaction.commit();
}

并且别忘了给它打电话。

无需使用framgment的FrameLayout作为容器..