【问题标题】:Gridview two columns per row without spacingGridview每行两列无间距
【发布时间】:2015-12-25 10:45:40
【问题描述】:

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

我的是image

我想去除图像之间的空白

这是类文件电影片段

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 的 xml 文件

<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"
    />

主活动类

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>

【问题讨论】:

  • set android:numColumns="2" -- ImageView -- 你需要设置(ScaleType CENTER) -- 尝试并反馈
  • 不,您创建一个新布局,例如(网格)并在您的网格适配器中膨胀它
  • 不,它没有用
  • 你能用你当前的代码发布你的输出屏幕吗?
  • 感谢您的帮助,现在我使用 center_crop 而不是 center

标签: android gridview imageview


【解决方案1】:

使用 center_crop 而不是 center

【讨论】:

    猜你喜欢
    • 2012-07-21
    • 2017-03-20
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多