Обновление WebView во фрагменте из элемента RecyclerView - PullRequest
0 голосов
/ 14 октября 2019

Я пытаюсь обновить свой WebView во фрагменте при нажатии на элемент RecyclerView. В консоли отладки он печатает мой жестко запрограммированный URL-адрес, но не обновляет представление - он остается на веб-странице Google без каких-либо изменений, но, похоже, onClickListener работает.

MainActivity.java

public class MainActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

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

    // Add Websites
    ArrayList<Website> websitesList = new ArrayList<>();
    websitesList.add(new Website(R.drawable.ic_launcher_background, "Line 1", "https://google.com/"));
    websitesList.add(new Website(R.drawable.ic_launcher_background, "Line 2", "https://yahoo.com/"));
    websitesList.add(new Website(R.drawable.ic_launcher_background, "Line 3", "https://abv.bg/"));

    mRecyclerView = findViewById(R.id.recyclerView);
    mLayoutManager = new LinearLayoutManager(this);
    mAdapter = new WebsiteAdapter(websitesList);

    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
    }

    public void switchContent(int id, Fragment fragment) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(id, fragment);
        ft.commit();
    }
}

Адаптер RecyclerView

@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
    final Website currentItem = mWebsiteList.get(position);

    holder.mImageView.setImageResource(currentItem.getImage());
    holder.mTitle.setText(currentItem.getTitle());

    holder.mTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fragmentJump(currentItem);
        }
    });
}

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

private void fragmentJump(Website mItemSelected) {
    Fragment mFragment = new WebsiteView();
    Bundle mBundle = new Bundle();
    mBundle.putString("url", "http://facebook.com/");
    mFragment.setArguments(mBundle);
    switchContent(R.id.fragment, mFragment);
}

public void switchContent(int id, Fragment fragment) {
    if (mContext == null)
        return;
    if (mContext instanceof MainActivity) {
        MainActivity mainActivity = (MainActivity) mContext;
        mainActivity.switchContent(id, fragment);
    }

}

Фрагмент WebView

private WebView webView;
String mUrl;

public WebsiteView() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_website_view, container, false);

    try {
        mUrl = getArguments().getString("url");
    } catch(Exception ex) {
        System.out.println(ex.toString());
        mUrl = "http://google.com/";
    }

    System.out.println(mUrl);

    webView = v.findViewById(R.id.webView);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl(mUrl);

    return v;
}

1 Ответ

0 голосов
/ 14 октября 2019

Хорошо, оказалось, что я на самом деле пытаюсь обновить данные фрагмента, и когда я заменяю другой фрагмент на нужный, он работает. Есть идеи, как обновить WebView во фрагменте?

...