2016-06-23 8 views
0

Dies ist, was ich meine, indem zweimal angezeigt wird. Beide können scrollen, je nachdem, wo Sie Ihren Finger platzieren.Android View Anzeige zweimal

enter image description here

Ich habe einen Grundadapter, der nichts anderes tut, als die Ansicht geladen werden (die oben gesehen einige Platzhalter-Daten hat).

Hier ist, wo die Ansicht in dem Adapter verwiesen:

@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; 
} 

Und der Adapter ist Aufruf in einem Fragment hier:

@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; 
} 

Ich habe auch einen Cursor-Loader in diesem Fragment, das Swaps der Cursor onLoadFinished und onLoaderReset.

Meine layout.xml-Datei, nur für den Fall, dass es relevant ist. Dies ist der main_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> 

public class MainActivity extends AppCompatActivity { 

    String LOG_TAG = MainActivity.class.getSimpleName(); 

    private final String FORECASTFRAGMENT_TAG = "FFTAG"; 
    String mLocation; 


    private void showMap(Uri zipCode){ 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setData(zipCode); 

     if(intent.resolveActivity(this.getPackageManager()) != null){ 
      startActivity(intent); 
     }else{ 
      View view = getCurrentFocus(); 
      Snackbar.make(view, "No Map application found", Snackbar.LENGTH_SHORT).setAction("", null).show(); 
     } 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
//  super.onCreate(savedInstanceState); 
//  setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
// 
//  mLocation = Utility.getPreferredLocation(this); 
// 
//  Log.v(LOG_TAG, "onCreate()"); 

     mLocation = Utility.getPreferredLocation(this); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.fragment, new ForecastFragment(), FORECASTFRAGMENT_TAG) 
        .commit(); 
     } 

    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     Log.v(LOG_TAG, "onDestroy()"); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Log.v(LOG_TAG, "onStart()"); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if(mLocation != Utility.getPreferredLocation(this)){ 
      mLocation = Utility.getPreferredLocation(this); 
      ForecastFragment ff = (ForecastFragment)getSupportFragmentManager().findFragmentByTag(FORECASTFRAGMENT_TAG); 
      ff.onLocationChange(); 
     } 
     Log.v(LOG_TAG, "onResume()"); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     Log.v(LOG_TAG, "onStop()"); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     Log.v(LOG_TAG, "onPause()"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 

     Context context = getApplicationContext(); 
//  int duration = Toast.LENGTH_SHORT; 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
//   Toast toast = Toast.makeText(context, "Settings Pressed", duration); 
//   toast.show(); 

      Intent intent = new Intent(context, SettingsActivity.class); 
      startActivity(intent); 

      return true; 
     }else if(id == R.id.action_map_view){ 
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplication()); 
      String zipCode = preferences.getString(getString(
        R.string.pref_location_key), 
        getString(R.string.pref_location_default)); 


      Uri geolocation = Uri.parse("geo:0,0?").buildUpon() 
        .appendQueryParameter("q", zipCode) 
        .build(); 

      showMap(geolocation); 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

Und das ist die content_main.xml, auf die verwiesen wird:

<?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> 

für meine MainActivity.java gestellte

<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" /> 
+1

Warum verwenden Sie RecyclerView nicht? –

+0

Können Sie den Code für Ihre Aktivität angeben?Das Problem ist wahrscheinlich dort – user3331142

+0

Just posted die Aktivität @ user3331142. – Cagrosso

Antwort

2

Ich habe diese Art von Verhalten gesehen vorher mit Fragmenten. Dies bedeutet, dass Sie mehr als ein Fragment haben, das an die Aktivität angehängt wurde. Jedes mit einer eigenen funktionierenden Listenansicht. Ich bin mir nicht sicher, wie dies in Ihrem Code abläuft.

EDIT

Zwei Fragmente in den folgenden Stellen angebracht werden.

Einmal im 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" /> 

Und einmal in Ihrer Aktivität:

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

Um dies zu beheben, entfernen Sie

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

in Ihrem onCreate() Ihre Tätigkeit und Sie‘ Ich habe jetzt nur eine Liste angezeigt.

+0

Hat dies Ihre Frage beantwortet? – user3331142

+1

Es tat, danke! – Cagrosso

1

Sie brauchen nicht das Fragment in der onCreate hinzuzufügen, da Sie es in der XML erklärt haben:

entfernen diese von Ihrem onCreate:

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

und machen es diese:

@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); 

}