这就是我显示两次的意思。它们都可以根据您放置手指的位置滚动。
我有一个基本的适配器,除了加载视图(上面有一些占位符数据)之外什么都不做。
以下是适配器中引用视图的地方:
pyplot.figure(figsize=(5,5))
images = create_dummy_images()
for i, image in enumerate(images):
pyplot.subplot(2, 2, i + 1)
pyplot.axis("off")
pyplot.imshow(image, aspect='auto')
pyplot.subplots_adjust(hspace=0, wspace=0)
pyplot.show()
适配器在这里调用一个片段:
@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;
}
我在该片段中还有一个游标加载器,用于将游标交换到LoadFinished和onLoaderReset。
我的layout.xml文件,以防它相关。
@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;
}
要求我的MainActivity.java:
<?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>
这是main_activity.xml
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);
}
}
这是引用的content_main.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>
答案 0 :(得分:2)
我之前已经看过片段之前的这种行为。它表示您有多个已附加到活动的片段。每个都有自己的功能列表视图。我不确定你的代码中是如何发生这种情况的。
修改强>
以下地方附有两个碎片。
进入你的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();
要解决此问题,请删除
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, new ForecastFragment(), FORECASTFRAGMENT_TAG)
.commit();
在您的onCreate()
活动中,您现在只会显示一个列表。
答案 1 :(得分:1)
您不需要在onCreate
中添加片段,因为您已在xml中声明了它:
从onCreate
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);
}