是否可以在cardview或recyclerview中使webview响应?

时间:2017-04-01 06:38:32

标签: android android-layout

MainActivity.java

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Context mContext;
    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    Button start;
   // TextView textView;
    RequestQueue requestQueue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        start = (Button)findViewById(R.id.button);
        requestQueue = Volley.newRequestQueue(this);
        start.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(final View view) {
                JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(Request.Method.GET, "http://192.168.0.103:3001", new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            System.out.println(response);
                            for(int i = 0; i < response.length();i++){

                                JSONObject json_res = response.getJSONObject(i);

                                RecyclerAdapter.embed_html.add(json_res.getString("v.embed_html"));

                            }

                        //Recycler View Setup
                        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
                        layoutManager = new LinearLayoutManager(MainActivity.this);
                        recyclerView.setLayoutManager(layoutManager);
                        RecyclerAdapter adapter = new RecyclerAdapter(getApplicationContext());
                        recyclerView.setAdapter(adapter);
                        }catch (JSONException e){
                            e.printStackTrace();
                            System.out.println(e);                        }
                    }
                    },
                    new Response.ErrorListener(){
                        @Override
                                public void onErrorResponse(VolleyError error){
                                    System.out.println(error);
                        }
                }
                );
               requestQueue.add(jsonObjectRequest);
            }
        });

    }
}

RecyclerAdapter.java

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import newsee.novasyslabs.com.nodefetch.Others.CircularTransform;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    Context mContext;
    public static ArrayList <String> embed_html = new ArrayList<>();

    public RecyclerAdapter(Context mContext){
        this.mContext = mContext;
    }

    class ViewHolder extends RecyclerView.ViewHolder{


        WebView webView;
        public ViewHolder(View itemView) {
            super(itemView);
            this.webView = (WebView) itemView.findViewById(R.id.web);

        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.layout_card, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
            holder.webView.getSettings().setJavaScriptEnabled(true);
            holder.webView.loadData(embed_html.get(position), "text/html", "utf-8");

        }
    @Override
    public int getItemCount() {
        return embed_html.size();
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="newsee.novasyslabs.com.nodefetch.MainActivity">

    <Button
        android:text="Request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_alignStart="@+id/appBarLayout"
        />


    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/appBarLayout" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appBarLayout"
        android:layout_marginTop="82dp"
        />


</RelativeLayout>

layout_card.xml

<?xml version="1.0" encoding="utf-8" ?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
            <WebView
                android:id="@+id/web"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:background="#000000"
                android:layout_alignParentTop="true"
                />

</android.support.v7.widget.CardView>

我试图在cardview中设置我的网页视图并且它工作正常但是我面对的是当我在5.5英寸手机上运行时它会离开屏幕并且当我运行我的代码时平板电脑覆盖整个屏幕。 我希望它像。它适用于具有wrap_content高度的卡片,并在我在任何设备上运行app时填充父卡片。 Tab或Mobile。 请帮帮我,我是android新手。 在此先感谢!!!

click here to see image

0 个答案:

没有答案