2 qq 33451652 qq_33451652 于 2016.04.20 10:34 提问

求教Fragment和ListView点击跳转
 package gfxy.com.fragment_dahb;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Web extends Fragment {
    WebView we;
    EditText ed1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.web,null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getActivity().setContentView(R.layout.web);
        Button bu = (Button) getActivity().findViewById(R.id.webbut);
        ed1 = (EditText) getActivity().findViewById(R.id.webed);
        we = (WebView) getActivity().findViewById(R.id.webweb);
        WebSettings wes = we.getSettings();
        wes.setJavaScriptEnabled(true);
        bu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setweb();
            }
        });
        we.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    Toast.makeText(getActivity(), "网页加载完成", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), "加载中", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_BACK) && we.canGoBack()){
            we.goBack();
            return true;
        }
        return false;
    }
    public void setweb() {
        we.loadUrl("http://" + ed1.getText().toString().trim());
        we.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }
}

 package gfxy.com.fragment_dahb;

import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioGroup;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

/**
 * Created by Administrator on 2016/4/18.
 */
public class bt extends Fragment {

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FrameLayout sa = (FrameLayout) getActivity().findViewById(R.id.fm);
        sa.removeAllViews();
        CategorySeries dataset=buildCategoryDataset("测试饼图");
        GraphicalView graphicalView=ChartFactory.getPieChartView(getActivity(),dataset, renderer);//饼状图
        sa.addView(graphicalView, new RadioGroup.LayoutParams(RadioGroup.LayoutParams.FILL_PARENT, RadioGroup.LayoutParams.FILL_PARENT));
    }
    protected CategorySeries buildCategoryDataset(String title) {
        CategorySeries categorySeries = new CategorySeries(title);
        categorySeries.add("胜 ", 30);
        categorySeries.add("负", 20);
        categorySeries.add("平", 50);
        return categorySeries;
    }
    int[] colors={Color.BLUE,Color.GREEN,Color.MAGENTA,Color.RED};
    DefaultRenderer renderer=buildCategoryRenderer(colors);

    protected DefaultRenderer buildCategoryRenderer(int[] colors) {
        DefaultRenderer renderer = new DefaultRenderer();
        renderer.setLegendTextSize(20);//设置左下角表注的文字大小
        //renderer.setZoomButtonsVisible(true);//设置显示放大缩小按钮
        renderer.setZoomEnabled(false);//设置不允许放大缩小.
        renderer.setChartTitleTextSize(30);//设置图表标题的文字大小
        renderer.setChartTitle("统计结果");//设置图表的标题  默认是居中顶部显示
        renderer.setLabelsTextSize(20);//饼图上标记文字的字体大小
        //renderer.setLabelsColor(Color.WHITE);//饼图上标记文字的颜色
        renderer.setPanEnabled(false);//设置是否可以平移
        //renderer.setDisplayValues(true);//是否显示值
        renderer.setClickEnabled(true);//设置是否可以被点击
        renderer.setMargins(new int[]{20, 30, 15, 0});
        //margins - an array containing the margin size values, in this order: top, left, bottom, right
        for (int color : colors) {
            SimpleSeriesRenderer r = new SimpleSeriesRenderer();
            r.setColor(color);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }
}

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="sadsd"
            android:textSize="25dp"
            android:gravity="center"
            android:background="#0f4"/>

    </LinearLayout>

    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/lv"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@null"
            android:choiceMode="singleChoice">

        </ListView>

        <FrameLayout
            android:id="@+id/fm"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

我想把Web。和bt显示在framelayout里面,求教怎么做?

还有ListView的点击跳转事件之前用还没问题,也不知道干了什么,然后就不能进行跳转了

求解决??

 package gfxy.com.fragment_dahb;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

    private String[] name = new String[]{
            "饼图","静态折线","动态折线","Web","逐帧动画","缩放,透明,旋转,平移"
    };
    private FragmentManager fm;
    private FragmentTransaction ft;
    private FrameLayout fml;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fml = (FrameLayout) findViewById(R.id.fm);
        ListView lv = (ListView)findViewById(R.id.lv);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for (int i=0;i<name.length;i++){
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("name",name[i]);
            list.add(map);
        }
        lv.setAdapter(new SimpleAdapter(this,list,R.layout.content_main,new String[]{"name"},new int[]{R.id.text1}));
//        int wi = getWindowManager().getDefaultDisplay().getWidth();//获取手机宽度
//        int hi = getWindowManager().getDefaultDisplay().getHeight();//获取手机高度
        fm = getFragmentManager();//创建一个Fragment
        ft = fm.beginTransaction();//对Fragment进行添加,删除,替换
        ft.replace(R.id.fm,new Fragment1());
        ft.commit();

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position==0){
                    ft = fm.beginTransaction();
                    ft.replace(R.id.fm,new bt());
                    ft.commit();
                }
                if(position==1){
                    ft = fm.beginTransaction();
                    ft.replace(R.id.fm,new Fragment1());
                    ft.commit();
                }
                if(position==3){
                    ft = fm.beginTransaction();
                    ft.replace(R.id.fm,new Web());
                    ft.commit();
                }
            }
        });
    }
}

2个回答

CSDNXIAOD
CSDNXIAOD   2016.04.20 10:42
已采纳

底部导航栏点击跳转fragment
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

q610098308
q610098308   2016.04.25 22:49

你的问题是什么??,只写代码,没有问题?

qq_33451652
qq_33451652 这段时间没看,不好意思了,已经解决了
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!