Gridview每行两列没有间距

时间:2015-12-25 10:45:41

标签: android gridview imageview

我试图在GridView中使用自定义适配器显示两个图像,并使用毕加索库显示图像,但我在图像之间有空格,我希望应用看起来像App mock

我的是image

我想删除图像之间的白色间距

这是类File MovieFragment

package com.example.good.movieapp;

public class MovieFragment extends Fragment {

String[] movieTitle, moviePosterPath;

public MovieFragment() {
    // Required empty public constructor
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

}

ImageAdapter mImageAdater;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_movie, container, false);

    mImageAdater = new ImageAdapter(getActivity());
    GridView movieView = (GridView) rootView.findViewById(R.id.movieview);
    movieView.setAdapter(mImageAdater);
    updateMovie();
    movieView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MovieFragment.this.getActivity(), "" + position,
                    Toast.LENGTH_SHORT).show();
        }
    });

    return rootView;

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}

private void updateMovie(){
    FetchMovie fetchMovie = new FetchMovie();
    fetchMovie.execute();
}


public class FetchMovie extends AsyncTask<Void, Void, List<String>>{
    private final String LOG_TAG = FetchMovie.class.getSimpleName();

    private List<String> getMovies(String jsonString) throws JSONException{
        JSONObject movieJson = new JSONObject(jsonString);
        JSONArray movieArray = movieJson.getJSONArray("results");
        List<String> urls = new ArrayList<>();
        for(int i=0; i<movieArray.length(); i++){
            JSONObject movie = movieArray.getJSONObject(i);
            urls.add("http://image.tmdb.org/t/p/w342" + movie.getString("poster_path"));
        }
        return urls;
    }

    @Override
    protected List<String> doInBackground(Void... params) {

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

        String jsonString = null;
        String sort_by = "popularity.desc";
        int page = 1;

        try{

            final String BASE_URL = "https://api.themoviedb.org/3/discover/movie?";
            final String PAGE_PARAM = "page";
            final String SORT_BY_PARAM = "popularity.desc";
            final String APP_ID = "api_key";

            Uri builtUri = Uri.parse(BASE_URL).buildUpon()
                    .appendQueryParameter(PAGE_PARAM, Integer.toString(page))
                    .appendQueryParameter(SORT_BY_PARAM, sort_by)
                    .appendQueryParameter(APP_ID, BuildConfig.MOVIE_API_KEY)
                    .build();

            URL url = new URL(builtUri.toString());

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            if(inputStream==null){
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while((line = reader.readLine()) != null){
                buffer.append(line + "\n");

            }
            if(buffer.length()==0){
                return null;
            }
            jsonString = buffer.toString();


        }catch (final IOException e){
            Log.e(LOG_TAG, "Error closing stream", e);
            return null;
        }finally{
            if(urlConnection != null){
                urlConnection.disconnect();
            }
            if(reader!=null){
                try{
                    reader.close();
                }catch(final IOException e){
                    Log.e(LOG_TAG, "error closing reader", e);
                }
            }
        }

        try {
            return getMovies(jsonString);
        }catch (JSONException j){
            Log.e(LOG_TAG, j.getMessage(), j);
            j.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(List<String> strings) {
        mImageAdater.replace(strings);
    }

}

public class ImageAdapter extends BaseAdapter{
    private final String LOG_TAG = ImageAdapter.class.getSimpleName();
    private final Context context;
    private final List<String> urls = new ArrayList<String>();

        public ImageAdapter(Context context){
        this.context = context;
        //Collections.addAll(urls, moviePosterPath);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = new ImageView(context);

        }
        ImageView movieView = (ImageView) convertView;

        String url = getItem(position);

        Log.e(LOG_TAG, "URL: " + url);

        Picasso.with(context).load(url).into(movieView);
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public String getItem(int position) {
        return urls.get(position);
    }

    @Override
    public int getCount() {
        return urls.size();
    }
    public void replace(List<String> urls) {
        this.urls.clear();
        this.urls.addAll(urls);
        notifyDataSetChanged();
    }
}

}

这是MovieFragment的

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.good.movieapp.MovieFragment">

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/movieview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    />

MainActivityClass

package com.example.good.movieapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //populate the fragment
    if(savedInstanceState == null){
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MovieFragment())
                .commit();
    }
}

MainActivity 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.good.movieapp.MainActivity"
android:id="@+id/container">


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

使用了center_crop而不是center