Android视图显示两次

Android视图显示两次,android,android-linearlayout,Android,Android Linearlayout,这就是我所说的显示两次的意思。它们都可以根据手指的位置滚动 我有一个基本的适配器,除了加载视图(上面有一些占位符数据)之外,它什么都不做 以下是在适配器中引用视图的位置: @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { View view = LayoutInflater.from(context).inflate(R.layout.list_item_forec

这就是我所说的显示两次的意思。它们都可以根据手指的位置滚动

我有一个基本的适配器,除了加载视图(上面有一些占位符数据)之外,它什么都不做

以下是在适配器中引用视图的位置:

    @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = LayoutInflater.from(context).inflate(R.layout.list_item_forecast, parent, false);

    return view;
}
适配器在此处以片段形式调用:

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

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    mForecastAdapter = new ForecastAdapter(getActivity(), null, 0);


    ListView forecastListView = (ListView) rootView.findViewById(R.id.listView_forecast);
    forecastListView.setAdapter(mForecastAdapter);

    forecastListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView adapterView, View view, int position, long l) {
            // CursorAdapter returns a cursor at the correct position for getItem(), or null
            // if it cannot seek to that position.
            Cursor cursor = (Cursor) adapterView.getItemAtPosition(position);
            if (cursor != null) {
                String locationSetting = Utility.getPreferredLocation(getActivity());
                Intent intent = new Intent(getActivity(), DetailActivity.class)
                        .setData(WeatherContract.WeatherEntry.buildWeatherLocationWithDate(
                                locationSetting, cursor.getLong(COL_WEATHER_DATE)
                        ));
                startActivity(intent);
            }
        }
    });

    return rootView;
}
我在片段中还有一个游标加载程序,它将游标onLoadFinished和onLoaderReset交换

我的layout.xml文件,以防万一

<?xml version="1.0" encoding="utf-8"?>

<!-- Layout for weather forecast list item for future day (not today) -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:padding="16dp">

    <ImageView
        android:id="@+id/list_item_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:paddingLeft="16dp">

        <TextView
            android:id="@+id/list_item_date_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tomorrow"/>

        <TextView
            android:id="@+id/list_item_forecast_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Clear"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/list_item_high_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="81"/>

        <TextView
            android:id="@+id/list_item_low_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="68"/>
    </LinearLayout>

</LinearLayout>
这是主要的_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.android.sunshine.app.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <!--<android.support.design.widget.FloatingActionButton-->
        <!--android:id="@+id/fab"-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_gravity="bottom|end"-->
        <!--android:layout_margin="@dimen/fab_margin"-->
        <!--android:src="@android:drawable/ic_dialog_email" />-->

</android.support.design.widget.CoordinatorLayout>

这是引用的content_main.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name="com.example.android.sunshine.app.ForecastFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main" />

我以前在片段中见过这种行为。它表示您有多个已附加到活动的片段。每个都有自己的功能列表视图。我不确定在你的代码中这是如何发生的

编辑

两个碎片被固定在以下位置

一旦在xml中:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name="com.example.android.sunshine.app.ForecastFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main" />
要解决此问题,请删除

getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment, new ForecastFragment(), FORECASTFRAGMENT_TAG)
                    .commit();

在活动的
onCreate()
中,现在只显示一个列表。

我以前见过这种带有片段的行为。它表示您有多个已附加到活动的片段。每个都有自己的功能列表视图。我不确定在你的代码中这是如何发生的

编辑

两个碎片被固定在以下位置

一旦在xml中:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment"
    android:name="com.example.android.sunshine.app.ForecastFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main" />
要解决此问题,请删除

getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment, new ForecastFragment(), FORECASTFRAGMENT_TAG)
                    .commit();

在活动的
onCreate()
中,现在只显示一个列表。

您不需要在
onCreate
中添加片段,因为您已经在xml中声明了它:

从创建时的
中删除此项:

  if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment, new ForecastFragment(),FORECASTFRAGMENT_TAG)
                .commit();
    }
这样做:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mLocation = Utility.getPreferredLocation(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

您不需要在
onCreate
中添加片段,因为您已经在xml中声明了它:

从创建时的
中删除此项:

  if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment, new ForecastFragment(),FORECASTFRAGMENT_TAG)
                .commit();
    }
这样做:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mLocation = Utility.getPreferredLocation(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

为什么不使用RecyclerView?能否为您的活动提供代码?问题可能是刚刚发布了activity@user3331142。至于RecyclerView,我正在遵循一个教程,这就是他们在本例中使用的@DarushDaryView。您还可以显示您的主要活动xml吗?为什么不使用RecyclerView?您可以提供活动的代码吗?问题可能是刚刚发布了activity@user3331142。至于RecyclerView,我正在遵循一个教程,这就是他们在本例中使用的@darushdarycoull显示您的主要活动xml吗?这回答了您的问题吗?这回答了您的问题吗?