【问题标题】:Fragment over listview activity [duplicate]列表视图活动的片段[重复]
【发布时间】:2017-03-30 05:16:44
【问题描述】:

我对 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);
}

这是点击

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();
    }
}

【问题讨论】:

  • 属于主题,但您可以考虑显示新活动,而不是添加片段,如果这可能会使您的代码更清晰,则在创建时将必要的数据传递给它。..
  • 片段的视图组是什么意思?
  • 请不要重复提问。只需使用您拥有的任何新信息、您尝试过的任何新代码或任何已发布答案不起作用的解释来编辑您的原始帖子,就会将它推到活动队列的顶部。

标签: android xml listview android-activity fragment


【解决方案1】:

为您的活动 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>

并在FragmentTransaction中传递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作为容器..

【讨论】:

  • 所以基本上有两件事要改变(1)来自Activity的容器(2)需要提交fragmentTrasaction,试试这个,如果你觉得有用,不要忘记检查它是否为真..谢谢。 .
  • 我试过了,但它似乎不起作用。虽然它不会使应用程序崩溃,但它不会打开新片段
猜你喜欢
  • 2020-10-05
  • 1970-01-01
  • 2018-04-01
  • 2015-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多