我尝试使用NavigationDrawer
进行新闻体育应用。但是当我打开应用程序时,我想显示WebView
,然后从左到后显示ListView
。我做了一些事情,但现在ListView
没有显示这些项目。我有4个班级和2个xml。
package com.baronescu.sportsnews2;
import android.os.Bundle;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
String[] menu;
DrawerLayout dLayout;
ListView dList;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu = new String[]{"Football","Tennis","Basketball","Hockey","Handball"};
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
dList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
dLayout.closeDrawers();
Bundle args = new Bundle();
args.putString("Menu", menu[position]);
Fragment detail = new DetailFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.dfurl, detail).commit();
}
});
Thread timer = new Thread(){
public void run (){
try {
sleep(2000);
} catch (InterruptedException e ){
e.printStackTrace();
} finally {
Intent odu = new Intent ("com.baronescu.sportsnews2.DEFAULTURL");
startActivity(odu);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
package com.baronescu.sportsnews2;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class DetailFragment extends Fragment {
TextView text;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.menu_detail_fragment, container, false);
String menu = getArguments().getString("Menu");
text= (TextView) view.findViewById(R.id.detail);
text.setText(menu);
return view;
}
}
package com.baronescu.sportsnews2;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import com.baronescu.sportsnews2.OurViewClient;
public class DefaultUrl extends Activity {
WebView firstnews;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstnews = (WebView) findViewById(R.id.dfurl);
firstnews.setWebViewClient(new OurViewClient());
firstnews.getSettings().setJavaScriptEnabled(true);
firstnews.getSettings().setLoadWithOverviewMode(true);
firstnews.getSettings().setUseWideViewPort(true);
try {
firstnews.loadUrl("http://www.digisport.ro");
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.baronescu.sportsnews2;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class OurViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView v, String url) {
// TODO Auto-generated method stub
v.loadUrl(url);
return true;
}
}
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sportsnews" >
<!-- android:id="@+id/content_frame" -->
<!-- android:layout_width="match_parent" -->
<!-- android:layout_height="match_parent" > -->
<WebView
android:id="@+id/dfurl"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#ffffff"
android:textSize="40px" />
</LinearLayout>
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baronescu.sportsnews2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DefaultUrl"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.baronescu.sportsnews2.DEFAULTURL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>